Minimum Moves to Equal Array Elements - Problem
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment n - 1 elements of the array by 1.
Note: Incrementing n - 1 elements is equivalent to decrementing one element by 1.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3]
›
Output:
3
💡 Note:
Need 3 moves: increment [1,2] by 1 (→[2,3,3]), increment [2,3] by 1 (→[3,4,3]), increment [3,4] by 1 (→[4,4,4]). Total: 3 moves.
Example 2 — All Equal
$
Input:
nums = [1,1,1]
›
Output:
0
💡 Note:
All elements are already equal, so no moves needed.
Example 3 — Larger Differences
$
Input:
nums = [1,2,9]
›
Output:
10
💡 Note:
Minimum is 1. Moves needed: (2-1) + (9-1) = 1 + 8 = 9 moves to equalize all to 1.
Constraints
- 2 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array with different element values
2
Key Insight
Incrementing n-1 elements = decrementing 1 element
3
Final State
All elements equal to minimum value
Key Takeaway
🎯 Key Insight: Instead of incrementing n-1 elements, think of it as decrementing 1 element to reach the minimum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code