# C#LeetCode刷题之#453-最小移动次数使数组元素相等（Minimum Moves to Equal Array Elements）

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

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n – 1 elements by 1.

Input:[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]

```public class Program {

public static void Main(string[] args) {
var x = new int[] { 1, 2, 3 };

var res = MinMoves(x);
Console.WriteLine(res);

x = new int[] { 1, 2147483647 };
res = MinMoves2(x);
Console.WriteLine(res);

x = new int[] { 1, 5, 7, 2, 3 };
res = MinMoves3(x);
Console.WriteLine(res);

}

private static int MinMoves(int[] nums) {
//这个解法未AC，因为nums.Sum()的值有可能会超出整型范围
return nums.Sum() - nums.Length * nums.Min();
}

private static int MinMoves2(int[] nums) {
var min = nums.Min();
var sum = 0L;
foreach (var num in nums) {
sum += num;
}
return (int)(sum - min * nums.Length);
}

private static int MinMoves3(int[] nums) {
var min = nums.Min();
return nums.Sum(n => n - min);
}

}```

```3
2147483646
13```