- Math knowledge: for
[a, b], find a num that has the shortest distance to both nums. It must be
betweena and b.
- Now we have
len / 2pairs, ofc we need to
sortthem first, then find the
- The median must be a num that is between every pair!
- When len is odd, it’s the middle num.
- When len is even, it’s the average of the 2 middle nums.
Given an integer array
nums of size
n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment or decrement an element of the array by 1.
Test cases are designed so that the answer will fit in a
Input: nums = [1,2,3]