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:
The brute force strategy is to simulate the process one move at a time. We repeatedly find the largest number and increase all the other numbers by one until every number in the collection is the same.
Here's how the algorithm would work step-by-step:
def min_moves_brute_force(numbers):
number_of_moves = 0
list_size = len(numbers)
if list_size == 0:
return 0
while True:
# This check is necessary to determine if the array elements are equal, which is our stop condition.
all_elements_are_equal = all(element == numbers[0] for element in numbers)
if all_elements_are_equal:
break
maximum_number = -float('inf')
index_of_maximum = -1
for current_index in range(list_size):
if numbers[current_index] > maximum_number:
maximum_number = numbers[current_index]
index_of_maximum = current_index
# Incrementing all elements except the max is the core logic of a single "move".
for current_index in range(list_size):
if current_index != index_of_maximum:
numbers[current_index] += 1
number_of_moves += 1
return number_of_movesInstead of simulating the process of increasing numbers one by one, we can find the solution with a simple calculation. The key insight is that increasing all but one number is the same as decreasing that one number relative to the others.
Here's how the algorithm would work step-by-step:
def min_moves_to_equal_array(numbers_list):
# Increasing n-1 elements is equivalent to decreasing one element, so we find the minimum element.
minimum_number = min(numbers_list)
total_moves = 0
# Each number must be reduced to the minimum, and each reduction is one move.
for current_number in numbers_list:
# The difference between any number and the minimum is the exact number of moves for that element.
total_moves += current_number - minimum_number
return total_moves| Case | How to Handle |
|---|---|
| Input array with only one element | The solution should return 0 as all elements are already equal and no moves are needed. |
| Input array where all elements are already equal | The algorithm correctly calculates the sum of differences from the minimum, which is zero, returning 0. |
| Input array containing negative numbers | The logic of incrementing n-1 elements remains the same, so the solution works correctly without modification. |
| Input array with a mix of positive, negative, and zero values | The relative differences between elements are what matter, so the presence of zeros or mixed signs is handled correctly by the standard algorithm. |
| Input array with large numbers, potentially causing integer overflow | The sum of differences could exceed the capacity of a standard 32-bit integer, so a 64-bit integer type (like a long) should be used for calculations. |
| Maximum-sized input array, leading to performance issues | An optimal O(n) solution involving a single pass to find the minimum and sum is required to avoid a timeout on large inputs. |
| Input array with large differences between min and max elements | Even with large differences, the mathematical insight that moves equal sum(nums) - n * min(nums) holds and prevents simulation, which would be too slow. |
| Input array containing duplicate values | Duplicates are handled naturally by the summation approach as each element's difference from the minimum is added independently. |