You are given two arrays of positive integers, p1 and p2, representing players in a racing game. The two arrays are sorted, non-empty, and have the same length, n. The i-th element of each array corresponds to where that player was on the track at the i-th second of the race. We know that:
- player 1 started ahead (
p1[0] > p2[0]), - player 2 overtook player 1 once, and
- player 2 remained ahead until the end (
p1[n - 1] < p2[n - 1]).
Assume the arrays have no duplicates, and that p1[i] != p2[i] for any index.
Return the index at which player 2 overtook player 1.
Example:
- Input:
p1 = [2, 4, 6, 8, 10], p2 = [1, 3, 5, 9, 11] - Output:
3
l
2
0
4
1
mid
6
2
8
3
r
10
4
p2=[1, 3, 5, 9, 11]
Step 1 / 4
Step 1:
Initialize l=0, r=4. Compute mid=2.
Pointers: l=0, mid=2, r=4
Focus: select @ [2]