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 or decrement an element of the array by 1.
Test cases are designed so that the answer will fit in a 32-bit integer.
Example 1:
Input: nums = [1,2,3] Output: 2 Explanation: Only two moves are needed (remember each move increments or decrements one element): [1,2,3] => [2,2,3] => [2,2,2]
Example 2:
Input: nums = [1,10,2,9] Output: 16
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 method for this problem involves trying out every single number as the target to which all other numbers in the list will be changed. We calculate the number of moves needed for each potential target and find the lowest number of moves overall.
Here's how the algorithm would work step-by-step:
def minimum_moves_brute_force(numbers):
minimum_number_of_moves = float('inf')
# Consider each number as a potential final value.
for potential_final_value in numbers:
total_moves_for_final_value = 0
# Calculate moves needed to change each number to potential final value.
for number in numbers:
total_moves_for_final_value += abs(number - potential_final_value)
# Update minimum moves if current potential final value yields fewer moves.
minimum_number_of_moves = min(minimum_number_of_moves, total_moves_for_final_value)
return minimum_number_of_movesThe trick to this problem is realizing that all the numbers should move towards a single meeting point to minimize the total movement. The best meeting point is the middle number once you sort the list.
Here's how the algorithm would work step-by-step:
def minimum_moves_to_equal_array_elements_ii(numbers):
numbers.sort()
# The median is the optimal meeting point to minimize total moves
list_length = len(numbers)
median_index = list_length // 2
median_value = numbers[median_index]
total_moves = 0
for number in numbers:
# Sum the absolute differences from each number to the median
total_moves += abs(number - median_value)
return total_moves| Case | How to Handle |
|---|---|
| Empty or null input array | Return 0 if the input array is empty or null, as no moves are required. |
| Array with a single element | Return 0 if the array has only one element, as it is already equal to itself. |
| Array with all elements being the same | Return 0 as no moves are needed to make all elements equal. |
| Array with positive and negative numbers | The algorithm should correctly calculate the moves needed irrespective of the sign of the numbers, as it finds the median and calculates absolute differences. |
| Array with very large numbers (close to integer limits) that might cause overflow during summation | Use long data type to store intermediate sums of differences to prevent integer overflow. |
| Array with many duplicate numbers | The median calculation and move calculation handle duplicate numbers correctly. |
| Large input array affecting performance | Using a linear time median finding algorithm (e.g., quickselect or using the nth_element method) ensures efficient performance for large inputs, avoiding O(n log n) sorting. |
| Array where the median is not an integer | When the array size is even, calculate the median as the average of the two middle elements; since all moves are integer, either of the two middle elements will yield the optimal result. |