Java simple solution using Median


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

Log in to reply
 

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