FunctionRacerProblemsRace

Swaps for Two Rising Lists

HardArraysPython, JavaScript, TypeScript, Java, C++

top and bottom have the same length. One move picks a position i and swaps top[i] with bottom[i].

Return the fewest moves after which both lists are strictly increasing (every item larger than the one before it), or -1 if no moves can do that.

Example 1

top [2,4,9,7]
bottom [1,3,6,10]
returns 1

Swap the last position: the lists become 2, 4, 9, 10 and 1, 3, 6, 7.

Example 2

top [5,3]
bottom [6,3]
returns -1

Position 1 holds 3 and 3: whichever way round, one list has 3 right after its 5.

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