462. Minimum Moves to Equal Array Elements II
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public int minMoves2(int[] nums) {
if(nums == null || nums.length <= 1) {
return 0;
}
Arrays.sort(nums);
int moves = 0, median = nums[nums.length/2];
for(int num: nums) {
moves += Math.abs(num-median);
}
return moves;
}
}