[LeetCode] Minimum Moves to Equal Array Elements III

3736. Minimum Moves to Equal Array Elements III

You are given an integer array nums.

In one move, you may increase the value of any single element nums[i] by 1.

Return the minimum total number of moves required so that all elements in nums become equal.

1
2
3
4
5
6
class Solution {
public:
int minMoves(vector<int>& nums) {
return *max_element(begin(nums), end(nums)) * nums.size() - accumulate(begin(nums), end(nums), 0);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/21/PS/LeetCode/minimum-moves-to-equal-array-elements-iii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.