FunctionRacerProblemsRace

Fewest Changes to a Run

HardTwo pointersPython, JavaScript, TypeScript, Java, C++

A list is a run when its items are all different and, once sorted, each is exactly 1 more than the one before it. One change replaces one item of nums by any whole number.

Return the fewest changes after which the items of nums, in some order, form a run.

Example 1

nums [10,3,11,12]
returns 1

Change the 3 to 9 or to 13. The items are then four numbers in a row.

Example 2

nums [5,5,5]
returns 2

Two of the 5s must change, for example to 4 and 6.

Example 3

nums [3,1,2]
returns 0

Sorted, the items are 1, 2, 3 already.

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 Two pointers problems