Minimum Moves to Equal Array Elements - Problem
Imagine you have an array of integers, and you want to make all elements equal using a special type of move. In each move, you can increment all but one element by 1. Your goal is to find the minimum number of moves required to achieve this equality.
This problem tests your mathematical insight - while the brute force approach might simulate the actual moves, the optimal solution requires recognizing a clever mathematical relationship that transforms this seemingly complex problem into a simple calculation.
Example: Given [1, 2, 3], you need 3 moves:
Move 1: increment first two → [2, 3, 3]
Move 2: increment first two → [3, 4, 3]
Move 3: increment last two → [3, 4, 4]
Input & Output
example_1.py — Basic Case
$
Input:
[1,2,3]
›
Output:
3
💡 Note:
We need 3 moves: [1,2,3] → [2,3,3] → [3,4,3] → [4,4,4]. Each move increments n-1 elements.
example_2.py — Already Equal
$
Input:
[1,1,1]
›
Output:
0
💡 Note:
All elements are already equal, so no moves are needed.
example_3.py — Large Differences
$
Input:
[1,2,10]
›
Output:
10
💡 Note:
Using the formula: (1-1) + (2-1) + (10-1) = 0 + 1 + 9 = 10 moves needed.
Constraints
- n == nums.length
- 1 ≤ n ≤ 105
- -109 ≤ nums[i] ≤ 109
- The answer is guaranteed to fit in a 32-bit integer
Visualization
Tap to expand
Understanding the Visualization
1
Identify Target Level
Find the minimum element - this is our target level that all elements should reach
2
Calculate Individual Needs
For each element, calculate how many levels it needs to 'drop' to reach the minimum
3
Sum All Requirements
The total moves needed is the sum of all individual drop requirements
Key Takeaway
🎯 Key Insight: Instead of thinking about raising n-1 elements up, think about how much each element needs to come down to the minimum level!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code