n: top and bottom. Elements with the same index form a pair, and in one operation you may pick an index i and swap top[i] with bottom[i].
You want the last element of each array to be its maximum: top[n - 1] is at least every element of top, and bottom[n - 1] is at least every element of bottom.
Return the minimum number of swaps that satisfies both conditions, or -1 if that is impossible.top=[2, 8, 3], bottom=[6, 1, 9] → 1
Swap the pair at index 1: top = [2, 1, 3], bottom = [6, 8, 9]. Now 3 is the maximum of top and 9 is the maximum of bottom.
top=[4, 1], bottom=[1, 4] → 1
A single swap is enough: either the first pair or the last one.
top=[5, 2], bottom=[6, 3] → -1
The pair (5, 6) beats both numbers of the last pair whichever way it is placed, so the condition cannot be met.
top=[2, 8, 3], bottom=[6, 1, 9]
1
Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.