Short Java Solution


  • 0
    B
    public int minMoves2(int[] nums) {
            Arrays.sort(nums);
            int mid = nums.length / 2;
            int res = 0;
            for(int i = 0; i < nums.length; i++) {
                res += Math.abs(nums[i] - nums[mid]);
            }
            return res;
            
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.