#C7162. Last Friend Holding the Potato
Last Friend Holding the Potato
Last Friend Holding the Potato
You are given (N) friends standing in a row, numbered from 1 to (N). Each friend has a pass strength given by an integer array (P) of length (N). The game starts with the first friend (position 1) holding a potato. At each step, the friend currently holding the potato passes it to the friend at position (\min(i + P[i], N)) where (i) is the current friend’s 1-indexed position. The process repeats until no further pass can be made (i.e. when the computed next position is not greater than the current position). Your task is to determine the 1-indexed position of the last friend holding the potato.
Note: In the formula, if the friend currently at position (i) has a pass strength (P[i]), then the next friend is at position (\min(i + P[i], N)). If (i + P[i] \leq i) (or if (P[i] = 0)), the passing stops.
inputFormat
The first line contains an integer (T) denoting the number of test cases. Each test case consists of two lines: the first line contains an integer (N) (the number of friends) and the second line contains (N) space-separated integers representing the pass strengths (P[1], P[2], \dots, P[N]).
outputFormat
For each test case, output a single line containing the 1-indexed position of the last friend holding the potato.## sample
4
4
3 1 2 4
3
1 2 0
5
10 1 2 3 4
1
0
4
3
5
1
</p>