Minimum Moves to Equal Array Elements (LeetCode 453): Solution and Explanation
One move adds 1 to all but one element of nums. Find the fewest moves that make every element equal. The answer is sum(nums) - n * min(nums).
Intuition
Simulating moves is hopeless with values up to 10^9, so look at what a move does to the differences between elements.
When n - 1 elements go up by 1 and one stays, every difference among the raised elements is unchanged. Only the skipped element falls 1 further behind the others.
Since only differences matter for being equal, a move is the same as lowering one element by 1.
Lowering is cheapest when everything meets at the current minimum, which never needs to move. Each element costs exactly its height above the minimum.
Approach: Sum of distances to the minimum
- Find
low, the smallest element. - Add up
x - lowfor every elementx, using a 64-bit accumulator in fixed-width languages. - Return the sum.
Step-by-step example
nums = [1,2,3]
- One real move, raising everything except 3, gives [2,3,3]: the 3 got 1 closer to the rest, as if it had been lowered.
- The minimum is 1.
- 1 adds 0, 2 adds 1, 3 adds 2.
- Total 0 + 1 + 2 = 3 moves.
Code: Python, Java, C++
1class Solution:
2 def minMoves(self, nums: List[int]) -> int:
3 low = min(nums)
4 moves = 0
5 for x in nums:
6 moves += x - low
7 return moves1class Solution {
2 public int minMoves(int[] nums) {
3 int low = Integer.MAX_VALUE;
4 for (int x : nums) low = Math.min(low, x);
5 long moves = 0;
6 for (int x : nums) {
7 moves += (long) x - low;
8 }
9 return (int) moves;
10 }
11}1class Solution {
2public:
3 int minMoves(vector<int>& nums) {
4 int low = *min_element(nums.begin(), nums.end());
5 long long moves = 0;
6 for (int x : nums) {
7 moves += (long long) x - low;
8 }
9 return (int) moves;
10 }
11};Time and space complexity
O(n)
One pass to find the minimum and one pass to add the gaps.
O(1)
Only the minimum and the running total are stored.
Other approaches
Closed form (time O(n), space O(1))
Compute sum(nums) - n * min(nums) directly. It is the same formula in one line; watch for overflow of the sum in 32-bit languages.
Sort and accumulate (time O(n log n), space O(1))
Sort the array and reason about raising the smaller elements to meet the next one. It reaches the same answer with extra O(n log n) work.
Simulate (time O(answer * n), space O(1))
Repeatedly raise all but the maximum. Correct but the number of moves can be about 2 * 10^9, so it times out.
Common mistakes
- Overflow. A single gap can be 2 * 10^9, more than a 32-bit int holds, so accumulate in a 64-bit integer.
- Targeting the maximum or the average. The final common value is min + moves, which is usually higher than the current maximum, so thinking in terms of raising toward a fixed target is confusing. Think in terms of lowering to the minimum.
- Confusing this with Minimum Moves II, where a move changes one element by 1 in either direction and the median is the right target.
Frequently asked questions
Why is lowering one element equivalent to raising all the others?
Equality only depends on the differences between elements. Raising n - 1 elements by 1 changes the differences in exactly the same way as lowering the remaining element by 1.
What value do all elements end at?
min(nums) + moves. Every move raises the minimum element (it is never the one skipped in an optimal plan), so it climbs by exactly the number of moves.
How does this differ from Minimum Moves to Equal Array Elements II?
In part II a move changes a single element by 1, up or down. Then the best meeting point is the median, and the answer is the sum of distances to it.