From feb0f443b88fc37a18be0f8cf681c89f31b47327 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 May 2026 10:51:07 +0300 Subject: [PATCH 1/4] Added tasks 3780, 3781, 3782 --- .../Solution.java | 51 ++++++++++++++++++ .../readme.md | 39 ++++++++++++++ .../Solution.java | 24 +++++++++ .../readme.md | 43 +++++++++++++++ .../Solution.java | 11 ++++ .../readme.md | 53 +++++++++++++++++++ .../SolutionTest.java | 18 +++++++ .../SolutionTest.java | 18 +++++++ .../SolutionTest.java | 23 ++++++++ 9 files changed, 280 insertions(+) create mode 100644 src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/Solution.java create mode 100644 src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/readme.md create mode 100644 src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java create mode 100644 src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/readme.md create mode 100644 src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java create mode 100644 src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/readme.md create mode 100644 src/test/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/Solution.java b/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/Solution.java new file mode 100644 index 000000000..44c78a026 --- /dev/null +++ b/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/Solution.java @@ -0,0 +1,51 @@ +package g3701_3800.s3780_maximum_sum_of_three_numbers_divisible_by_three; + +// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Senior #Biweekly_Contest_172 +// #2026_05_06_Time_7_ms_(98.17%)_Space_168.14_MB_(89.91%) + +public class Solution { + public int maximumSum(int[] nums) { + int[][] group = new int[3][3]; + for (int num : nums) { + int m = num % 3; + int max1 = group[m][0]; + int max2 = group[m][1]; + int max3 = group[m][2]; + if (num >= max1) { + max3 = max2; + max2 = max1; + max1 = num; + } else if (num >= max2) { + max3 = max2; + max2 = num; + } else if (num > max3) { + max3 = num; + } + group[m][0] = max1; + group[m][1] = max2; + group[m][2] = max3; + } + int res = 0; + for (int[] g : group) { + int sum = 0; + for (int i = 0; i < 3; i += 1) { + if (g[i] == 0) { + sum = 0; + break; + } + sum += g[i]; + } + res = Math.max(res, sum); + } + int max = 0; + for (int[] g : group) { + if (g[0] == 0) { + max = 0; + break; + } + max += g[0]; + } + res = Math.max(res, max); + return res; + } +} diff --git a/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/readme.md b/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/readme.md new file mode 100644 index 000000000..fce6203bd --- /dev/null +++ b/src/main/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/readme.md @@ -0,0 +1,39 @@ +3780\. Maximum Sum of Three Numbers Divisible by Three + +Medium + +You are given an integer array `nums`. + +Your task is to choose **exactly three** integers from `nums` such that their sum is divisible by three. + +Return the **maximum** possible sum of such a triplet. If no such triplet exists, return 0. + +**Example 1:** + +**Input:** nums = [4,2,3,1] + +**Output:** 9 + +**Explanation:** + +The valid triplets whose sum is divisible by 3 are: + +* `(4, 2, 3)` with a sum of `4 + 2 + 3 = 9`. +* `(2, 3, 1)` with a sum of `2 + 3 + 1 = 6`. + +Thus, the answer is 9. + +**Example 2:** + +**Input:** nums = [2,1,5] + +**Output:** 0 + +**Explanation:** + +No triplet forms a sum divisible by 3, so the answer is 0. + +**Constraints:** + +* 3 <= nums.length <= 105 +* 1 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java new file mode 100644 index 000000000..c583389bd --- /dev/null +++ b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java @@ -0,0 +1,24 @@ +package g3701_3800.s3781_maximum_score_after_binary_swaps; + +// #Medium #Array #String #Greedy #Heap_Priority_Queue #Staff #Biweekly_Contest_172 +// #2026_05_06_Time_39_ms_(96.47%)_Space_118.00_MB_(29.41%) + +public class Solution { + public long maximumScore(int[] nums, String s) { + long sum = 0; + PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); + for (int i = 0; i < nums.length; i++) { + if (s.charAt(i) == '1') { + if (pq.isEmpty() || pq.peek() <= nums[i]) { + sum += nums[i]; + } else { + sum += pq.poll(); + pq.add(nums[i]); + } + } else { + pq.add(nums[i]); + } + } + return sum; + } +} diff --git a/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/readme.md b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/readme.md new file mode 100644 index 000000000..af9c40b40 --- /dev/null +++ b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/readme.md @@ -0,0 +1,43 @@ +3781\. Maximum Score After Binary Swaps + +Medium + +You are given an integer array `nums` of length `n` and a binary string `s` of the same length. + +Initially, your score is 0. Each index `i` where `s[i] = '1'` contributes `nums[i]` to the score. + +You may perform **any** number of operations (including zero). In one operation, you may choose an index `i` such that `0 <= i < n - 1`, where `s[i] = '0'`, and `s[i + 1] = '1'`, and swap these two characters. + +Return an integer denoting the **maximum possible score** you can achieve. + +**Example 1:** + +**Input:** nums = [2,1,5,2,3], s = "01010" + +**Output:** 7 + +**Explanation:** + +We can perform the following swaps: + +* Swap at index `i = 0`: `"01010"` changes to `"10010"` +* Swap at index `i = 2`: `"10010"` changes to `"10100"` + +Positions 0 and 2 contain `'1'`, contributing `nums[0] + nums[2] = 2 + 5 = 7`. This is maximum score achievable. + +**Example 2:** + +**Input:** nums = [4,7,2,9], s = "0000" + +**Output:** 0 + +**Explanation:** + +There are no `'1'` characters in `s`, so no swaps can be performed. The score remains 0. + +**Constraints:** + +* `n == nums.length == s.length` +* 1 <= n <= 105 +* 1 <= nums[i] <= 109 +* `s[i]` is either `'0'` or `'1'` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java new file mode 100644 index 000000000..092b74466 --- /dev/null +++ b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java @@ -0,0 +1,11 @@ +package g3701_3800.s3782_last_remaining_integer_after_alternating_deletion_operations; + +// #Hard #Math #Recursion #Senior_Staff #Biweekly_Contest_172 +// #2026_05_06_Time_1_ms_(100.00%)_Space_43.05_MB_(9.38%) + +public class Solution { + public long lastInteger(long n) { + final long MASK = 0xAAAAAAAAAAAAAAAL; + return ((n - 1) & MASK) + 1; + } +} diff --git a/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/readme.md b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/readme.md new file mode 100644 index 000000000..7278cda22 --- /dev/null +++ b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/readme.md @@ -0,0 +1,53 @@ +3782\. Last Remaining Integer After Alternating Deletion Operations + +Hard + +You are given an integer `n`. + +We write the integers from 1 to `n` in a sequence from left to right. Then, **alternately** apply the following two operations until only one integer remains, starting with operation 1: + +* **Operation 1**: Starting from the left, delete every second number. +* **Operation 2**: Starting from the right, delete every second number. + +Return the last remaining integer. + +**Example 1:** + +**Input:** n = 8 + +**Output:** 3 + +**Explanation:** + +* Write `[1, 2, 3, 4, 5, 6, 7, 8]` in a sequence. +* Starting from the left, we delete every second number: [1, **2**, 3, **4**, 5, **6**, 7, **8**]. The remaining integers are `[1, 3, 5, 7]`. +* Starting from the right, we delete every second number: [**1**, 3, **5**, 7]. The remaining integers are `[3, 7]`. +* Starting from the left, we delete every second number: [3, **7**]. The remaining integer is `[3]`. + +**Example 2:** + +**Input:** n = 5 + +**Output:** 1 + +**Explanation:** + +* Write `[1, 2, 3, 4, 5]` in a sequence. +* Starting from the left, we delete every second number: [1, **2**, 3, **4**, 5]. The remaining integers are `[1, 3, 5]`. +* Starting from the right, we delete every second number: [1, **3**, 5]. The remaining integers are `[1, 5]`. +* Starting from the left, we delete every second number: [1, **5**]. The remaining integer is `[1]`. + +**Example 3:** + +**Input:** n = 1 + +**Output:** 1 + +**Explanation:** + +* Write `[1]` in a sequence. +* The last remaining integer is 1. + +**Constraints:** + +* 1 <= n <= 1015 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/SolutionTest.java b/src/test/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/SolutionTest.java new file mode 100644 index 000000000..54a0a6a6d --- /dev/null +++ b/src/test/java/g3701_3800/s3780_maximum_sum_of_three_numbers_divisible_by_three/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3780_maximum_sum_of_three_numbers_divisible_by_three; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumSum() { + assertThat(new Solution().maximumSum(new int[] {4, 2, 3, 1}), equalTo(9)); + } + + @Test + void maximumSum2() { + assertThat(new Solution().maximumSum(new int[] {2, 1, 5}), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java b/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java new file mode 100644 index 000000000..ab72656ed --- /dev/null +++ b/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3781_maximum_score_after_binary_swaps; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumScore() { + assertThat(new Solution().maximumScore(new int[] {2, 1, 5, 2, 3}, "01010"), equalTo(7)); + } + + @Test + void maximumScore2() { + assertThat(new Solution().maximumScore(new int[] {4, 7, 2, 9}, "0000"), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/SolutionTest.java b/src/test/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/SolutionTest.java new file mode 100644 index 000000000..6ac50cda0 --- /dev/null +++ b/src/test/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3782_last_remaining_integer_after_alternating_deletion_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void lastInteger() { + assertThat(new Solution().lastInteger(8L), equalTo(3L)); + } + + @Test + void lastInteger2() { + assertThat(new Solution().lastInteger(5L), equalTo(1L)); + } + + @Test + void lastInteger3() { + assertThat(new Solution().lastInteger(1L), equalTo(1L)); + } +} From ca7349607105f06851304c6eb3794ba4b6fd2ded Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 May 2026 10:53:48 +0300 Subject: [PATCH 2/4] Fixed style --- .../Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java index 092b74466..7da4eac23 100644 --- a/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java +++ b/src/main/java/g3701_3800/s3782_last_remaining_integer_after_alternating_deletion_operations/Solution.java @@ -5,7 +5,7 @@ public class Solution { public long lastInteger(long n) { - final long MASK = 0xAAAAAAAAAAAAAAAL; - return ((n - 1) & MASK) + 1; + final long mask = 0xAAAAAAAAAAAAAAAL; + return ((n - 1) & mask) + 1; } } From 44081dd120d1ae04cdcb097c20f62f737d08ca29 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 May 2026 10:54:53 +0300 Subject: [PATCH 3/4] Fixed compile --- .../s3781_maximum_score_after_binary_swaps/Solution.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java index c583389bd..5029d3fec 100644 --- a/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java +++ b/src/main/java/g3701_3800/s3781_maximum_score_after_binary_swaps/Solution.java @@ -3,6 +3,8 @@ // #Medium #Array #String #Greedy #Heap_Priority_Queue #Staff #Biweekly_Contest_172 // #2026_05_06_Time_39_ms_(96.47%)_Space_118.00_MB_(29.41%) +import java.util.PriorityQueue; + public class Solution { public long maximumScore(int[] nums, String s) { long sum = 0; From 4869cb210ac4853862e0917fa18477ab3668228b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 May 2026 10:57:32 +0300 Subject: [PATCH 4/4] Fixed test --- .../s3781_maximum_score_after_binary_swaps/SolutionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java b/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java index ab72656ed..dbe6b4895 100644 --- a/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java +++ b/src/test/java/g3701_3800/s3781_maximum_score_after_binary_swaps/SolutionTest.java @@ -8,11 +8,11 @@ class SolutionTest { @Test void maximumScore() { - assertThat(new Solution().maximumScore(new int[] {2, 1, 5, 2, 3}, "01010"), equalTo(7)); + assertThat(new Solution().maximumScore(new int[] {2, 1, 5, 2, 3}, "01010"), equalTo(7L)); } @Test void maximumScore2() { - assertThat(new Solution().maximumScore(new int[] {4, 7, 2, 9}, "0000"), equalTo(0)); + assertThat(new Solution().maximumScore(new int[] {4, 7, 2, 9}, "0000"), equalTo(0L)); } }