Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment n - 1 elements of the array by 1.
Example 1:
Input: nums = [1,2,3] Output: 3 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
Example 2:
Input: nums = [1,1,1] Output: 0
Constraints:
n == nums.length1 <= nums.length <= 105-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a group of items with different sizes, and you want to make them all exactly equal by adding tiny bits to them. The brute force strategy involves testing every single possible final target value, and for each one, calculating how much effort is needed to bring every single item up to that target.
Here's how the algorithm would work step-by-step:
def min_moves(nums):
if not nums:
return 0
minimum_possible_target = min(nums)
maximum_possible_target = max(nums) + len(nums)
minimum_total_effort = float('inf')
# We test every plausible final value as a goal to find the absolute minimum effort
for current_target_goal in range(minimum_possible_target, maximum_possible_target):
current_total_effort = 0
for item_value in nums:
current_total_effort += (current_target_goal - item_value)
# We track the lowest effort encountered across all tested target goals
if current_total_effort < minimum_total_effort:
minimum_total_effort = current_total_effort
# Returning the optimal effort ensures we meet the problem constraints using brute force
return minimum_total_effortInstead of trying to increase numbers to make them match, the clever trick is to realize that adding one to many numbers is the same as subtracting one from just the single smallest number. By shifting our perspective this way, we can figure out the total effort by simply measuring how far each item needs to drop to reach the level of the smallest one.
Here's how the algorithm would work step-by-step:
def min_moves(array_elements):
# Finding the absolute minimum is crucial because all elements will eventually be reduced to match this baseline value.
smallest_baseline = min(array_elements)
total_downward_steps = 0
# Summing individual gaps avoids simulating every single move, drastically improving time complexity.
for current_item in array_elements:
total_downward_steps += current_item - smallest_baseline
# The total accumulated downward steps directly equal the minimum moves required to equalize the array.
return total_downward_steps| Case | How to Handle |
|---|---|
| Array with a single element | The algorithm naturally computes zero moves since all elements are already equal and the sum of differences from the minimum is zero. |
| Array with all elements already equal | The minimum value equals all other elements, resulting in a difference of zero for every element and a total move count of zero. |
| Array containing negative numbers and mixed signs | Mathematical equivalence holds because incrementing n-1 elements is functionally identical to decrementing the single excluded element, allowing us to sum differences from the absolute minimum regardless of sign. |
| Extremely skewed distributions with a massive outlier | The linear scan to find the minimum value and subsequent accumulation of differences execute efficiently in O(n) time without intermediate overflows. |
| Maximum-sized inputs up to array constraints | An O(n) single-pass approach processing each element once ensures the solution scales efficiently and avoids timeout errors. |
| Integer overflow during the summation of moves | Standard 32-bit integers can exceed capacity if the sum of differences is very large, so the handling uses 64-bit integer accumulators to prevent overflow. |