453. Minimum Moves to Equal Array Elements
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of moves to make all elements in an array equal. A single move consists of incrementing n - 1 elements by 1, where n is the total number of elements.
Solution
Minimum Moves to Equal Array Elements in Python
class Solution:
def minMoves(self, nums: List[int]) -> int:
# The problem of incrementing n-1 elements by 1 to make them all equal
# is equivalent to decrementing 1 element by 1 to make them all equal.
# The target for everyone to become is the minimum element in the array.
# So, the total number of moves is the sum of differences between each element
# and the minimum element.
# moves = sum(nums[i] - min(nums)) for all i
if not nums:
return 0
# Find the minimum value in the list.
min_val = min(nums)
# Calculate the total moves by summing the differences.
# Python's integers handle arbitrary size, so overflow is not a concern for the sum.
moves = 0
for num in nums:
moves += num - min_val
return moves
# A more concise way to write the same logic in Python:
# return sum(nums) - len(nums) * min(nums)
Minimum Moves to Equal Array Elements in Java
class Solution {
public int minMoves(int[] nums) {
// The problem of incrementing n-1 elements by 1 to make them all equal
// is equivalent to decrementing 1 element by 1 to make them all equal.
// The target for everyone to become is the minimum element in the array.
// So, the total number of moves is the sum of differences between each element
// and the minimum element.
// moves = sum(nums[i] - min(nums)) for all i
if (nums.length == 0) {
return 0;
}
// Find the minimum element in the array.
int minVal = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < minVal) {
minVal = nums[i];
}
}
// Alternative using streams: int minVal = Arrays.stream(nums).min().getAsInt();
// Calculate the total moves.
// Use a long for the sum to prevent potential intermediate overflow,
// even though the final answer is guaranteed to fit in an int.
long moves = 0;
for (int num : nums) {
moves += (long)num - minVal;
}
return (int)moves;
}
}
Minimum Moves to Equal Array Elements in C++
class Solution {
public:
int minMoves(vector<int>& nums) {
// The problem of incrementing n-1 elements by 1 to make them all equal
// is equivalent to decrementing 1 element by 1 to make them all equal.
// The target for everyone to become is the minimum element in the array.
// So, the total number of moves is the sum of differences between each element
// and the minimum element.
// moves = sum(nums[i] - min(nums)) for all i
if (nums.empty()) {
return 0;
}
// Find the minimum element in the array.
// Using std::min_element is a concise way to do this.
int min_val = *min_element(nums.begin(), nums.end());
// Calculate the total number of moves required.
// Use long long for the sum to prevent potential overflow, although the problem
// guarantees the final result fits in a 32-bit integer.
long long moves = 0;
for (int num : nums) {
moves += num - min_val;
}
return static_cast<int>(moves);
}
};
Minimum Moves to Equal Array Elements in TypeScript
function minMoves(nums: number[]): number {
// The problem of incrementing n-1 elements by 1 to make them all equal
// is equivalent to decrementing 1 element by 1 to make them all equal.
// The target for everyone to become is the minimum element in the array.
// So, the total number of moves is the sum of differences between each element
// and the minimum element.
// moves = sum(nums[i] - min(nums)) for all i
if (nums.length === 0) {
return 0;
}
// Find the minimum value in the array.
// Using Math.min with the spread operator is a concise way.
const minVal = Math.min(...nums);
// Calculate the total moves by summing the differences from the minimum.
// JavaScript's `number` type is a 64-bit float and can safely handle
// the sum without overflow in this problem's constraints.
let moves = 0;
for (const num of nums) {
moves += num - minVal;
}
return moves;
// A more functional approach using reduce:
// const minVal = Math.min(...nums);
// return nums.reduce((acc, current) => acc + (current - minVal), 0);
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.