FunctionRacerProblemsRace

Partners Side by Side

HardArraysPython, JavaScript, TypeScript, Java, C++

An even number of people sit in a row, and row[i] is the number of the person in seat i. The people are numbered from 0, each number once. Persons 0 and 1 are partners, so are 2 and 3, then 4 and 5, and so on. The seats come in benches of two: seats 0 and 1, seats 2 and 3, and so on.

One move makes two people, sitting anywhere, trade seats. Return the fewest moves after which every bench holds two partners.

Example 1

row [4,0,5,1,2,3]
returns 1

Persons 0 and 5 trade seats: the benches become 4 and 5, 0 and 1, 2 and 3.

Example 2

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

Every bench already holds partners. Which of the two sits first does not matter.

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 Arrays problems