[LeetCode] Minimum Moves to Equal Array Elements

453. Minimum Moves to Equal Array Elements

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.

1
2
3
4
5
6
class Solution {
public:
int minMoves(vector<int>& nums) {
return accumulate(begin(nums), end(nums), 0, [min = *min_element(begin(nums), end(nums))](int sum, int num) { return sum + num - min; });
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/12/06/PS/LeetCode/minimum-moves-to-equal-array-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.