From 8c74e7e40db849e259d787f0293caba4fa4fe5ce Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:52:00 +0300 Subject: [PATCH 1/5] Added tasks 3774, 3775, 3776 --- .../Solution.java | 19 ++++++ .../readme.md | 42 +++++++++++++ .../Solution.java | 40 ++++++++++++ .../readme.md | 56 +++++++++++++++++ .../Solution.java | 30 +++++++++ .../readme.md | 63 +++++++++++++++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 23 +++++++ 9 files changed, 314 insertions(+) create mode 100644 src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/Solution.java create mode 100644 src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/readme.md create mode 100644 src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/Solution.java create mode 100644 src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/readme.md create mode 100644 src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java create mode 100644 src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/readme.md create mode 100644 src/test/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/Solution.java b/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/Solution.java new file mode 100644 index 000000000..535995397 --- /dev/null +++ b/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/Solution.java @@ -0,0 +1,19 @@ +package g3701_3800.s3774_absolute_difference_between_maximum_and_minimum_k_elements; + +// #Easy #Array #Sorting #Mid_Level #Weekly_Contest_480 +// #2026_05_08_Time_5_ms_(96.82%)_Space_46.97_MB_(28.79%) + +import java.util.Arrays; + +public class Solution { + public int absDifference(int[] nums, int k) { + Arrays.sort(nums); + int maxSum = 0; + int minSum = 0; + for (int i = 0, j = nums.length - 1; i < k; i++, j--) { + minSum += nums[i]; + maxSum += nums[j]; + } + return maxSum - minSum; + } +} diff --git a/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/readme.md b/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/readme.md new file mode 100644 index 000000000..7cde9ee43 --- /dev/null +++ b/src/main/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/readme.md @@ -0,0 +1,42 @@ +3774\. Absolute Difference Between Maximum and Minimum K Elements + +Easy + +You are given an integer array `nums` and an integer `k`. + +Find the absolute difference between: + +* the **sum** of the `k` **largest** elements in the array; and +* the **sum** of the `k` **smallest** elements in the array. + +Return an integer denoting this difference. + +**Example 1:** + +**Input:** nums = [5,2,2,4], k = 2 + +**Output:** 5 + +**Explanation:** + +* The `k = 2` largest elements are 4 and 5. Their sum is `4 + 5 = 9`. +* The `k = 2` smallest elements are 2 and 2. Their sum is `2 + 2 = 4`. +* The absolute difference is `abs(9 - 4) = 5`. + +**Example 2:** + +**Input:** nums = [100], k = 1 + +**Output:** 0 + +**Explanation:** + +* The largest element is 100. +* The smallest element is 100. +* The absolute difference is `abs(100 - 100) = 0`. + +**Constraints:** + +* `1 <= n == nums.length <= 100` +* `1 <= nums[i] <= 100` +* `1 <= k <= n` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/Solution.java b/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/Solution.java new file mode 100644 index 000000000..d465f88cf --- /dev/null +++ b/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/Solution.java @@ -0,0 +1,40 @@ +package g3701_3800.s3775_reverse_words_with_same_vowel_count; + +// #Medium #String #Two_Pointers #Simulation #Senior #Weekly_Contest_480 +// #2026_05_08_Time_41_ms_(96.14%)_Space_48.30_MB_(90.84%) + +public class Solution { + public String reverseWords(String s) { + char[] wrd = s.toCharArray(); + String vowels = "aeiou"; + int left = 0; + int right = 0; + int firstWrd = 0; + int anotherWrd = 0; + while (left < wrd.length) { + right = left; + anotherWrd = 0; + while (right < wrd.length && wrd[right] != ' ') { + if (vowels.indexOf(wrd[right]) != -1) { + if (left == 0) { + firstWrd++; + } else { + anotherWrd++; + } + } + right++; + } + if (left != 0 && anotherWrd == firstWrd) { + int l = left; + int r = right - 1; + while (l < r) { + char temp = wrd[r]; + wrd[r--] = wrd[l]; + wrd[l++] = temp; + } + } + left = right + 1; + } + return new String(wrd); + } +} diff --git a/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/readme.md b/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/readme.md new file mode 100644 index 000000000..a62206e37 --- /dev/null +++ b/src/main/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/readme.md @@ -0,0 +1,56 @@ +3775\. Reverse Words With Same Vowel Count + +Medium + +You are given a string `s` consisting of lowercase English words, each separated by a single space. + +Determine how many vowels appear in the **first** word. Then, reverse each following word that has the **same vowel count**. Leave all remaining words unchanged. + +Return the resulting string. + +Vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. + +**Example 1:** + +**Input:** s = "cat and mice" + +**Output:** "cat dna mice" + +**Explanation:** + +* The first word `"cat"` has 1 vowel. +* `"and"` has 1 vowel, so it is reversed to form `"dna"`. +* `"mice"` has 2 vowels, so it remains unchanged. +* Thus, the resulting string is `"cat dna mice"`. + +**Example 2:** + +**Input:** s = "book is nice" + +**Output:** "book is ecin" + +**Explanation:** + +* The first word `"book"` has 2 vowels. +* `"is"` has 1 vowel, so it remains unchanged. +* `"nice"` has 2 vowels, so it is reversed to form `"ecin"`. +* Thus, the resulting string is `"book is ecin"`. + +**Example 3:** + +**Input:** s = "banana healthy" + +**Output:** "banana healthy" + +**Explanation:** + +* The first word `"banana"` has 3 vowels. +* `"healthy"` has 2 vowels, so it remains unchanged. +* Thus, the resulting string is `"banana healthy"`. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists of lowercase English letters and spaces. +* Words in `s` are separated by a **single** space. +* `s` does **not** contain leading or trailing spaces. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java new file mode 100644 index 000000000..5369a51a4 --- /dev/null +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java @@ -0,0 +1,30 @@ +package g3701_3800.s3776_minimum_moves_to_balance_circular_array; + +// #Medium #Array #Sorting #Greedy #Staff #Weekly_Contest_480 +// #2026_05_08_Time_2_ms_(100.00%)_Space_139.96_MB_(20.95%) + +public class Solution { + public long minMoves(int[] balance) { + int n = balance.length; + int j = -1; + long total = 0, res = 0; + for (int i = 0; i < n; i++) { + if (balance[i] < 0) { + j = i; + } + total += balance[i]; + } + if (j == -1) { + return 0; + } + if (total < 0) { + return -1; + } + for (int d = 1; balance[j] < 0; ++d) { + long storage = balance[(j + d) % n] + balance[(j - d % n + n) % n]; + res += Math.min(-balance[j], storage) * d; + balance[j] += storage; + } + return res; + } +} diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/readme.md b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/readme.md new file mode 100644 index 000000000..585d2e057 --- /dev/null +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/readme.md @@ -0,0 +1,63 @@ +3776\. Minimum Moves to Balance Circular Array + +Medium + +You are given a **circular** array `balance` of length `n`, where `balance[i]` is the net balance of person `i`. + +In one move, a person can transfer **exactly** 1 unit of balance to either their left or right neighbor. + +Return the **minimum** number of moves required so that every person has a **non-negative** balance. If it is impossible, return `-1`. + +**Note**: You are guaranteed that **at most** 1 index has a **negative** balance initially. + +**Example 1:** + +**Input:** balance = [5,1,-4] + +**Output:** 4 + +**Explanation:** + +One optimal sequence of moves is: + +* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [5, 0, -3]` +* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [4, 0, -2]` +* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [3, 0, -1]` +* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [2, 0, 0]` + +Thus, the minimum number of moves required is 4. + +**Example 2:** + +**Input:** balance = [1,2,-5,2] + +**Output:** 6 + +**Explanation:** + +One optimal sequence of moves is: + +* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [1, 1, -4, 2]` +* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [1, 0, -3, 2]` +* Move 1 unit from `i = 3` to `i = 2`, resulting in `balance = [1, 0, -2, 1]` +* Move 1 unit from `i = 3` to `i = 2`, resulting in `balance = [1, 0, -1, 0]` +* Move 1 unit from `i = 0` to `i = 1`, resulting in `balance = [0, 1, -1, 0]` +* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [0, 0, 0, 0]` + +Thus, the minimum number of moves required is 6. + +**Example 3:** + +**Input:** balance = [-3,2] + +**Output:** \-1 + +**Explanation:** + +It is impossible to make all balances non-negative for `balance = [-3, 2]`, so the answer is -1. + +**Constraints:** + +* 1 <= n == balance.length <= 105 +* -109 <= balance[i] <= 109 +* There is at most one negative value in `balance` initially. \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/SolutionTest.java b/src/test/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/SolutionTest.java new file mode 100644 index 000000000..379b26e57 --- /dev/null +++ b/src/test/java/g3701_3800/s3774_absolute_difference_between_maximum_and_minimum_k_elements/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3774_absolute_difference_between_maximum_and_minimum_k_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void absDifference() { + assertThat(new Solution().absDifference(new int[] {5, 2, 2, 4}, 2), equalTo(5)); + } + + @Test + void absDifference2() { + assertThat(new Solution().absDifference(new int[] {100}, 1), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/SolutionTest.java b/src/test/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/SolutionTest.java new file mode 100644 index 000000000..4fa4491a0 --- /dev/null +++ b/src/test/java/g3701_3800/s3775_reverse_words_with_same_vowel_count/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3775_reverse_words_with_same_vowel_count; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void reverseWords() { + assertThat(new Solution().reverseWords("cat and mice"), equalTo("cat dna mice")); + } + + @Test + void reverseWords2() { + assertThat(new Solution().reverseWords("book is nice"), equalTo("book is ecin")); + } + + @Test + void reverseWords3() { + assertThat(new Solution().reverseWords("banana healthy"), equalTo("banana healthy")); + } +} diff --git a/src/test/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/SolutionTest.java b/src/test/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/SolutionTest.java new file mode 100644 index 000000000..520f095eb --- /dev/null +++ b/src/test/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3776_minimum_moves_to_balance_circular_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMoves() { + assertThat(new Solution().minMoves(new int[] {5, 1, -4}), equalTo(4L)); + } + + @Test + void minMoves2() { + assertThat(new Solution().minMoves(new int[] {1, 2, -5, 2}), equalTo(6L)); + } + + @Test + void minMoves3() { + assertThat(new Solution().minMoves(new int[] {-3, 2}), equalTo(-1L)); + } +} From 332007e19717c84ba41143641a26957fcee07477 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:53:53 +0300 Subject: [PATCH 2/5] Fixed style --- .../Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java index 5369a51a4..a669a92ed 100644 --- a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java @@ -7,7 +7,8 @@ public class Solution { public long minMoves(int[] balance) { int n = balance.length; int j = -1; - long total = 0, res = 0; + long total = 0; + long res = 0; for (int i = 0; i < n; i++) { if (balance[i] < 0) { j = i; From dcefa0430279e5a89679a9b86dccd1ff042f39d3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:56:57 +0300 Subject: [PATCH 3/5] Fixed codeql --- .../s3776_minimum_moves_to_balance_circular_array/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java index a669a92ed..34b81f234 100644 --- a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java @@ -24,7 +24,7 @@ public long minMoves(int[] balance) { for (int d = 1; balance[j] < 0; ++d) { long storage = balance[(j + d) % n] + balance[(j - d % n + n) % n]; res += Math.min(-balance[j], storage) * d; - balance[j] += storage; + balance[j] += (int) storage; } return res; } From e3b03c8a8894de8c0b22658447b2e1888f39c752 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:57:56 +0300 Subject: [PATCH 4/5] Fixed sonar --- .../s3776_minimum_moves_to_balance_circular_array/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java index 34b81f234..6da0db99c 100644 --- a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java @@ -22,7 +22,7 @@ public long minMoves(int[] balance) { return -1; } for (int d = 1; balance[j] < 0; ++d) { - long storage = balance[(j + d) % n] + balance[(j - d % n + n) % n]; + long storage = balance[(j + d) % n] + (long) balance[(j - d % n + n) % n]; res += Math.min(-balance[j], storage) * d; balance[j] += (int) storage; } From 31607c8246b0ab378425d174887957d69471649f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:59:35 +0300 Subject: [PATCH 5/5] Fixed codeql --- .../s3776_minimum_moves_to_balance_circular_array/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java index 6da0db99c..f0254745c 100644 --- a/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java +++ b/src/main/java/g3701_3800/s3776_minimum_moves_to_balance_circular_array/Solution.java @@ -23,7 +23,7 @@ public long minMoves(int[] balance) { } for (int d = 1; balance[j] < 0; ++d) { long storage = balance[(j + d) % n] + (long) balance[(j - d % n + n) % n]; - res += Math.min(-balance[j], storage) * d; + res += Math.min(-balance[j], (int) storage) * d; balance[j] += (int) storage; } return res;