# 453.minimum-moves-to-equal-array-elements

## Statement

``````输入：nums = [1,2,3]

[1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]
``````

``````输入：nums = [1,1,1]

``````

• `n == nums.length`
• `1 <= nums.length <= 105`
• `-109 <= nums[i] <= 109`
• 答案保证符合 32-bit 整数

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`
• The answer is guaranteed to fit in a 32-bit integer.

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
int minMoves(vector<int> &nums) {
int m = *min_element(all(nums));
int res = 0;

for (auto &a : nums) {
res += a - m;
}

return res;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````