Leetcode 453 Solution

This article provides solution to leetcode question 453 (minimum-moves-to-equal-array-elements)

https://leetcode.com/problems/minimum-moves-to-equal-array-elements

Solution

class Solution {
public:
    int minMoves(vector<int>& nums) {
        if (nums.size() == 0)
            return 0;

        int min_val = INT_MAX;

        for (auto it = nums.begin(); it != nums.end(); it++)
            min_val = min(min_val, *it);

        int total = 0;
        for (auto it = nums.begin(); it != nums.end(); it++)
            total += *it - min_val;

        return total;
    }
};