FunctionRacerProblemsRace

Minimum Moves to Equal Elements

MediumMathPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Math problems