462. Minimum Moves to Equal Array Elements II
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 move consists of incrementing or decrementing a single element by 1. The total number of moves is the sum of increments and decrements applied to all elements.
Solution
Minimum Moves to Equal Array Elements II in Python
class Solution:
def minMoves2(self, nums: List[int]) -> int:
# The problem is equivalent to finding a number x that minimizes the sum of
# absolute differences |nums[i] - x|. This number is the median of the array.
# Sort the array to find the median.
nums.sort()
# The median is the element at the middle index.
# For an array of length n, the median is at index n // 2 in a 0-indexed sorted array.
n = len(nums)
median = nums[n // 2]
# Calculate the total number of moves by summing the absolute differences.
# Python's integers handle arbitrary size, so overflow is not an issue for the sum.
moves = 0
for num in nums:
moves += abs(num - median)
return moves
# A more concise, Pythonic way to calculate the sum:
# return sum(abs(num - median) for num in nums)
Minimum Moves to Equal Array Elements II in Java
class Solution {
public int minMoves2(int[] nums) {
// The problem is equivalent to finding a number x that minimizes the sum of
// absolute differences |nums[i] - x|. This number is the median of the array.
// Sort the array to find the median.
Arrays.sort(nums);
// The median is the element at the middle index.
// For an array of length n, the median is at index n/2 in a 0-indexed sorted array.
int n = nums.length;
int median = nums[n / 2];
// Calculate the total number of moves.
// The sum can be large, so use a 64-bit integer (long) to prevent overflow.
long moves = 0;
for (int num : nums) {
moves += Math.abs(num - median);
}
return (int)moves;
}
}
Minimum Moves to Equal Array Elements II in C++
class Solution {
public:
int minMoves2(vector<int>& nums) {
// The problem is equivalent to finding a number x that minimizes the sum of
// absolute differences |nums[i] - x|. This number is the median of the array.
// Sort the array to find the median.
sort(nums.begin(), nums.end());
// The median is the element at the middle index.
// For an array of size n, the median is at index n/2 in a 0-indexed sorted array.
int n = nums.size();
int median = nums[n / 2];
// Calculate the total number of moves.
// The sum can be large, so use a 64-bit integer (long long) to prevent overflow.
long long moves = 0;
for (int num : nums) {
moves += abs((long long)num - median);
}
return (int)moves;
}
};
Minimum Moves to Equal Array Elements II in TypeScript
function minMoves2(nums: number[]): number {
// The problem is equivalent to finding a number x that minimizes the sum of
// absolute differences |nums[i] - x|. This number is the median of the array.
// Sort the array to find the median. The sort callback ensures numeric sorting.
nums.sort((a, b) => a - b);
// The median is the element at the middle index.
// For an array of length n, the median is at index floor(n/2) in a 0-indexed sorted array.
const n = nums.length;
const median = nums[Math.floor(n / 2)];
// Calculate the total number of moves by summing the absolute differences.
let moves = 0;
for (const num of nums) {
moves += Math.abs(num - median);
}
return moves;
// An alternative using Array.prototype.reduce:
// return nums.reduce((acc, current) => acc + Math.abs(current - median), 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.