---
# System prepended metadata

title: Leetcode 453. Minimum Moves to Equal Array Elements
tags: [Math]

---

# Leetcode 453. Minimum Moves to Equal Array Elements

Runtime: 1 ms
faster than 100.00% 

Memory Usage: 39.2 MB 
less than 82.24%

``` java=
class Solution {
    public int minMoves(int[] nums) {
        int count=0, s=0, min=Integer.MAX_VALUE, n=nums.length;
        
        for(int num:nums){
            min=Math.min(num,min);
            s+=num;
        }
        count=s-n*min;
        
        return count;
    }
}

/*
    [2,3,6,8]
     3 4 7 8
     4 5 8 8
     5 6 9 8
     6 7 9 9
     7 8 10 9
     8 9 10 10
     9 10 11 10
     10 11 11 11
     11 12 12 11
     12 13 12 12
     13 13 13 13
     => 11
     
     
     解題思維: 數學題
     設array總和為 s，設最後平衡時的值為 x，設次數為 count，設陣列中最小值為 min
     1. s+(n-1)*count = x*n
     2. min+count = x
     => count = s-n*min

*/
```
###### tags: `Math`