Skip to content

Commit b633b21

Browse files
committed
Added task 3736
1 parent 4ff07c9 commit b633b21

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3701_3800.s3736_minimum_moves_to_equal_array_elements_iii;
2+
3+
// #Easy #Array #Math #Biweekly_Contest_169
4+
5+
public class Solution {
6+
public int minMoves(int[] nums) {
7+
int max = Integer.MIN_VALUE;
8+
int sum = 0;
9+
for (int num : nums) {
10+
sum += num;
11+
if (num > max) {
12+
max = num;
13+
}
14+
}
15+
return max * nums.length - sum;
16+
}
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3736\. Minimum Moves to Equal Array Elements III
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
In one move, you may **increase** the value of any single element `nums[i]` by 1.
8+
9+
Return the **minimum total** number of **moves** required so that all elements in `nums` become **equal**.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,1,3]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
To make all elements equal:
20+
21+
* Increase `nums[0] = 2` by 1 to make it 3.
22+
* Increase `nums[1] = 1` by 1 to make it 2.
23+
* Increase `nums[1] = 2` by 1 to make it 3.
24+
25+
Now, all elements of `nums` are equal to 3. The minimum total moves is `3`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [4,4,5]
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
To make all elements equal:
36+
37+
* Increase `nums[0] = 4` by 1 to make it 5.
38+
* Increase `nums[1] = 4` by 1 to make it 5.
39+
40+
Now, all elements of `nums` are equal to 5. The minimum total moves is `2`.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 100`
45+
* `1 <= nums[i] <= 100`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3736_minimum_moves_to_equal_array_elements_iii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minMoves() {
11+
assertThat(new Solution().minMoves(new int[] {2, 1, 3}), equalTo(3));
12+
}
13+
14+
@Test
15+
void minMoves2() {
16+
assertThat(new Solution().minMoves(new int[] {4, 4, 5}), equalTo(2));
17+
}
18+
}

0 commit comments

Comments
 (0)