Minimum Moves to Equal Elements
In one move you may add 1 to or subtract 1 from any single item of nums. Return the fewest moves needed to make all the items equal.
Example 1
nums [1,2,3]
returns 2
Make all 2: one move on 1 and one on 3.
Example 2
nums [1,10,2,9]
returns 16
Sorted is 1, 2, 9, 10. Aim for 2: 1 + 0 + 7 + 8 = 16.
Constraints
1 <= len(nums) <= 30-50 <= nums[i] <= 50
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Math problems
- Multiply StringsMedium
- Power ModMedium
- Trailing Zeroes in FactorialMedium
- Twin PrimesMedium
- Valid SquareMedium
- Can the Pair Be ReachedHard
- Fewest Fuses to Find the Faulty ChipHard
- Kth Multiple of EitherHard