Problem

Ball Passing at Time K

FULLTIMEPHONE SCREEN

There are n friends numbered from 1 to n. You are given an array throwsTo, where throwsTo[i - 1] is the friend who receives the ball whenever friend i throws it.

At time 1, friend 1 is holding the ball. At each next time, the current holder throws the ball to the friend indicated by throwsTo.

Return the friend holding the ball at time k.

Examples
01 · Example 1
n = 6
throwsTo = [2,3,4,5,3,6]
k = 10
return = 4

The ball holders by time are 1, 2, 3, 4, 5, 3, 4, 5, 3, 4, so at time 10 friend 4 has the ball.

02 · Example 2
n = 3
throwsTo = [2,3,1]
k = 1
return = 1

At time 1, the initial holder is friend 1.

03 · Example 3
n = 4
throwsTo = [2,2,4,3]
k = 5
return = 2

Friend 1 throws to friend 2, and friend 2 throws to themself, so friend 2 holds the ball at every time after 1.

Constraints
  • n == throwsTo.length
  • 1 <= n <= 100000
  • 1 <= throwsTo[i] <= n
  • 1 <= k <= 10^18
More HackerRank problems
drafts saved locally
public int friendHoldingBall(int n, int[] throwsTo, long k) {
  // write your code here
}
n6
throwsTo[2,3,4,5,3,6]
k10
expected4
sign in to submit