Problem · Array

Throw The Ball

MediumFlexportFULLTIMEOA

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 of 1 to n, inclusive. The i^th friend will always throw the ball towards the friend given at receiver[i], and this will happen each second. Friend 1 always starts with the ball, and a player always throws to another player. Determine which friend has the ball after k seconds pass.

Note: Friends are numbered starting with 1.

Function Description

Complete the function throwTheBall in the editor.

throwTheBall has the following parameters:

  1. int receiver[n]: the i^th friend will throw the ball to the friend indicated in receiver[i]
  2. int seconds: the time in seconds that the game lasts

Returns

int: the friend holding the ball at time = seconds

Examples
01 · Example 1
receiver = [2, 4, 1, 5, 3]
seconds = 6
return = 2
Example 1 illustration
After 6 seconds, the ball will be with friend 2.
Constraints
  • 2 ≤ n ≤ 10^5
  • 1 ≤ receiver[i] ≤ n (receiver[i] ≠ i)
  • 1 ≤ seconds ≤ 10^12
More Flexport problems
drafts saved locally
public int throwTheBall(int[] receiver, int seconds) {
  // write your code here
}
receiver[2, 4, 1, 5, 3]
seconds6
expected2
checking account