Problem · Array

Throw The Ball

Learn this problem
MediumFlexport logoFlexportFULLTIMEOA

Problem 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:

  1. int receiver[n]: receiver[i] is the friend who receives the ball from the i-th friend.
  2. 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) → int

Examples

Example 1

receiver = [2, 4, 1, 5, 3]seconds = 6return = 2

After 6 seconds, the ball is 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, long seconds) {
  // write your code here
}
receiver[2, 4, 1, 5, 3]
seconds6
expected2
checking account