Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;
long 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] + (long) balance[(j - d % n + n) % n];
res += Math.min(-balance[j], (int) storage) * d;
balance[j] += (int) storage;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= n == balance.length <= 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= balance[i] <= 10<sup>9</sup></code>
* There is at most one negative value in `balance` initially.
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading