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
.
For example:
nums = [1,2,3]
should return 3
because [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
requires 3 movesnums = [1,1,1]
should return 0
because it already meets the criteriaWhat is the most efficient way to implement this function? What is the time and space complexity of your solution? Are there any edge cases to consider?
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.length
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
The naive approach would be to simulate the moves until all elements are equal. This would involve repeatedly incrementing n - 1
elements and checking if all elements are equal. However, this approach is highly inefficient, especially for large arrays and large differences between elements.
The key insight is to realize that incrementing n - 1
elements is equivalent to decrementing one element. So, instead of incrementing n - 1
elements, we can think of decrementing one element in each move. The target value to make all elements equal will be the minimum element in the array. Therefore, the number of moves is the sum of the differences between each element and the minimum element.
moves
to 0.moves
.moves
.def min_moves(nums):
min_num = min(nums)
moves = 0
for num in nums:
moves += num - min_num
return moves
# Example usage
nums1 = [1, 2, 3]
print(min_moves(nums1)) # Output: 3
nums2 = [1, 1, 1]
print(min_moves(nums2)) # Output: 0
The algorithm iterates through the array twice: once to find the minimum element and once to calculate the moves. Both iterations take O(n) time, where n is the length of the array. Therefore, the overall time complexity is O(n).
The algorithm uses only a few constant extra variables (min_num
, moves
). Therefore, the space complexity is O(1), which is constant.
def min_moves_edge_cases(nums):
if not nums: # Empty array
return 0
if len(nums) == 1: # Array with one element
return 0
min_num = min(nums)
moves = 0
for num in nums:
moves += num - min_num
return moves