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 <= 105
-109 <= nums[i] <= 109
This problem asks for the minimum number of moves to make all elements in an array equal. In each move, we increment n-1
elements by 1, where n
is the array's length. A mathematical approach provides an efficient solution.
The core idea is that every increment operation increases the sum of the array. Let's analyze it:
s
be the sum of the array elements.min
be the minimum element in the array.n
be the length of the array.k
be the minimum number of moves.After k
moves, all elements will be equal to min + k
. The final sum of the array will be n * (min + k)
. Since each move adds n-1
to the sum, the total sum after k
moves will also be s + k * (n-1)
.
Therefore, we can set up an equation:
s + k * (n - 1) = n * (min + k)
Solving for k
:
s + k * n - k = n * min + n * k
s - n * min = k
This means the minimum number of moves (k
) is simply the difference between the sum of the array and n
times the minimum element.
The code implementations below directly apply the derived formula: sum(nums) - min(nums) * len(nums)
. They efficiently calculate the sum and minimum element and then apply the formula.
class Solution:
def minMoves(self, nums: List[int]) -> int:
return sum(nums) - min(nums) * len(nums)
class Solution {
public int minMoves(int[] nums) {
int sum = 0;
int min = Integer.MAX_VALUE;
for (int num : nums) {
sum += num;
min = Math.min(min, num);
}
return sum - min * nums.length;
}
}
class Solution {
public:
int minMoves(vector<int>& nums) {
long long sum = 0;
int min = INT_MAX;
for (int num : nums) {
sum += num;
min = min(min, num);
}
return sum - (long long)min * nums.size();
}
};
func minMoves(nums []int) int {
sum := 0
min := math.MaxInt32
for _, num := range nums {
sum += num
if num < min {
min = num
}
}
return sum - min*len(nums)
}
/**
* @param {number[]} nums
* @return {number}
*/
var minMoves = function(nums) {
let sum = 0;
let min = Infinity;
for (let num of nums) {
sum += num;
min = Math.min(min, num);
}
return sum - min * nums.length;
};
function minMoves(nums: number[]): number {
let sum = 0;
let min = Infinity;
for (let num of nums) {
sum += num;
min = Math.min(min, num);
}
return sum - min * nums.length;
};
All these solutions achieve the optimal time and space complexity. They directly implement the mathematical solution derived above, offering a concise and efficient approach to the problem. Note the use of long long
in the C++ solution to avoid potential integer overflow issues for very large arrays. Similar considerations might be necessary in other languages depending on the input constraints.