Problem · Array
Throw The Ball
Learn this problemProblem statement
In a park, there are n friends standing in a random order, and they plan to throw a ball around. Each friend has a unique number in the range from 1 to n, inclusive. The i-th friend always throws the ball to the friend given by receiver[i], and one throw happens each second. Friend 1 starts with the ball, and every friend throws to another friend.
Determine which friend has the ball after seconds seconds.
Note: Friends are numbered starting from 1.
The function receives the following parameters:
int receiver[n]:receiver[i]is the friend who receives the ball from thei-th friend.long seconds: the number of seconds that the game lasts.
Return the int identifier of the friend holding the ball after seconds seconds.
Function
throwTheBall(receiver: int[], seconds: long) → intExamples
Example 1
receiver = [2, 4, 1, 5, 3]seconds = 6return = 2After 6 seconds, the ball is with friend 2.
Constraints
2 ≤ n ≤ 10^51 ≤ receiver[i] ≤ n (receiver[i] ≠ i)1 ≤ seconds ≤ 10^12