diff --git a/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/Solution.java b/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/Solution.java new file mode 100644 index 000000000..3fd69621d --- /dev/null +++ b/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/Solution.java @@ -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; + } +} diff --git a/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/readme.md b/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/readme.md new file mode 100644 index 000000000..a24bf08d8 --- /dev/null +++ b/src/main/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/readme.md @@ -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` \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/SolutionTest.java b/src/test/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/SolutionTest.java new file mode 100644 index 000000000..383e6edae --- /dev/null +++ b/src/test/java/g3701_3800/s3736_minimum_moves_to_equal_array_elements_iii/SolutionTest.java @@ -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)); + } +}