Minimum Moves to Equal Array Elements — Problem & Solution
Minimum Moves to Equal Array Elements solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Minimum Moves to Equal Array Elements is a medium coding problem on arrays and math, asked in Amazon and Indeed interviews.
- Difficulty: Medium
- Topics: Arrays, Math
- Asked at: Amazon, Indeed
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums of size n, return the minimum number of moves required to make all elements equal.
In one move you may increment n - 1 elements of the array by 1.
Example 1
Input: nums = [1,2,3]
Output: 3
Explanation: [1,2,3] → [2,3,3] → [3,4,3] → [4,4,4].
Example 2
Input: nums = [1,1,1]
Output: 0
Example 3
Input: nums = [1,2,3,4]
Output: 6
Constraints
1 <= nums.length <= 100000-1000 <= nums[i] <= 1000The answer fits in a 32-bit integer.
Follow-up: Incrementing n-1 elements is the same as doing what to the remaining one?
Hints for Minimum Moves to Equal Array Elements
Hint 1
Raising
n - 1elements by 1 leaves every difference between a raised element and the untouched one changed by exactly 1.Hint 2
So the move is equivalent to decrementing a single element by 1 — the gaps behave identically.
Hint 3
If every element must fall to the minimum, the total number of decrements is
sum - n * min.
How to solve Minimum Moves to Equal Array Elements
Adding 1 to all but one element shifts the whole array up by 1 and pulls one element down by 1 relative to the rest. Since only relative values matter, the move is exactly "decrement one element by 1".
Approach
- Reframe the move as a single decrement.
- Every element must end at the same value; the cheapest common target is the minimum, because no move can ever raise an element relative to the others.
- Answer
sum(nums) - n * min(nums)— the total number of unit decrements needed.
Why it works
Each decrement reduces sum - n * min by exactly 1 while min cannot decrease below the final target, so that quantity is both a lower bound on the number of moves and achievable by decrementing each element down to the minimum.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Trying to simulate the moves is O(range × n) and times out.
- The intermediate sum can overflow 32 bits during accumulation even though the answer fits — accumulate in 64 bits.
Reference solution
Python
from typing import List
def minMoves(nums: List[int]) -> int:
return sum(nums) - min(nums) * len(nums)Java
public static int minMoves(int[] nums) {
long sum = 0;
int min = nums[0];
for (int x : nums) {
if (x < min) min = x;
sum += x;
}
return (int) (sum - (long) min * nums.length);
}C++
int minMoves(vector<int>& nums) {
long long sum = 0;
int mn = nums[0];
for (int x : nums) {
if (x < mn) mn = x;
sum += x;
}
return (int) (sum - (long long) mn * (long long) nums.size());
}JavaScript
var minMoves = function(nums) {
let min = nums[0], sum = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] < min) min = nums[i];
sum += nums[i];
}
return sum - min * nums.length;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays