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,17 @@
package g3701_3800.s3736_minimum_moves_to_equal_array_elements_iii;

// #Easy #Array #Math #Biweekly_Contest_169 #2026_04_15_Time_1_ms_(99.59%)_Space_46.18_MB_(89.25%)

public class Solution {
public int minMoves(int[] nums) {
int max = Integer.MIN_VALUE;
int sum = 0;
for (int num : nums) {
sum += num;
if (num > max) {
max = num;
}
}
return max * nums.length - sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3736\. Minimum Moves to Equal Array Elements III

Easy

You are given an integer array `nums`.

In one move, you may **increase** the value of any single element `nums[i]` by 1.

Return the **minimum total** number of **moves** required so that all elements in `nums` become **equal**.

**Example 1:**

**Input:** nums = [2,1,3]

**Output:** 3

**Explanation:**

To make all elements equal:

* Increase `nums[0] = 2` by 1 to make it 3.
* Increase `nums[1] = 1` by 1 to make it 2.
* Increase `nums[1] = 2` by 1 to make it 3.

Now, all elements of `nums` are equal to 3. The minimum total moves is `3`.

**Example 2:**

**Input:** nums = [4,4,5]

**Output:** 2

**Explanation:**

To make all elements equal:

* Increase `nums[0] = 4` by 1 to make it 5.
* Increase `nums[1] = 4` by 1 to make it 5.

Now, all elements of `nums` are equal to 5. The minimum total moves is `2`.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3701_3800.s3736_minimum_moves_to_equal_array_elements_iii;

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[] {2, 1, 3}), equalTo(3));
}

@Test
void minMoves2() {
assertThat(new Solution().minMoves(new int[] {4, 4, 5}), equalTo(2));
}
}
Loading