Minimum Moves to Equal Array Elements II

Problem Id: 462 Difficulty: Medium


Intuition

Solution


class Solution:
    def minMoves2(self, nums: List[int]) -> int:
        nums.sort()
        mid = nums[len(nums) // 2]
        return sum([abs(num - mid) for num in nums])