// Minimum Moves to Equal Array Elements II

// Given a non-empty integer array, find the minimum number of moves required to make all array elements equal,
// where a move is incrementing a selected element by 1 or decrementing a selected element by 1.

// You may assume the array's length is at most 10,000.

// Example:

// Input:
// [1,2,3]

// Output:
// 2

// Explanation:
// Only two moves are needed (remember each move increments or decrements one element):

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

/**
 * @param {array[]} nums
 * @return {number}
 */


@@ -554,8 +554,480 @@
 r%7D%0A */%0A%0A
+var minMoves2 = function(nums) %7B%0A    if(nums ===null%7C%7C nums.length===0) return 0;%0A    var moves = Number.MAX_VALUE;%0A    nums.sort(function(a, b) %7B%0A        return a-b;%0A    %7D);%0A    var totalSum = 0,%0A        sum = 0;%0A    for(var i =0;i%3Cnums.length;i++)%7B%0A        totalSum += nums%5Bi%5D;%0A    %7D%0A    for(i =0;i%3Cnums.length;i++)%7B%0A        var m = (i-(nums.length-i-1)-1)*nums%5Bi%5D-sum+(totalSum-sum);%0A        moves = Math.min(m, moves);%0A        sum+=nums%5Bi%5D;%0A    %7D%0A    return moves;%0A%7D;%0A

add first solution: