FunctionRacerProblemsRace

Stones Into a Row

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

Stones lie on different whole positions of a line, and stones lists the positions. The stone on the smallest position and the stone on the largest position are the end stones. One move picks up an end stone and puts it on a free position where it is no longer an end stone, that is, somewhere between two other stones.

The game stops when no move is possible, which happens exactly when the stones lie on positions in a row. Return [fewest, most]: the fewest and the most moves the game can take.

Example 1

stones [3,6,8]
returns [1,2]

Fewest: move the 3 to 7, and the stones lie on 6, 7, 8. Most: move the 8 to 5, then the 6 to 4.

Example 2

stones [1,2,3]
returns [0,0]

The stones are in a row 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