Find the Child Who Has the Ball After K Seconds

You are given two positive integers n and k. There are n children numbered from 0 to n - 1 standing in a queue in order from left to right.

Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches either end of the line, i.e. child 0 or child n - 1, the direction of passing is reversed.

Return the number of the child who receives the ball after k seconds.

Note: This question is the same as 2582: Pass the Pillow.

Example 1
Inputn = 3, k = 5
Output1
After 5 seconds of passing back and forth among 3 children, child 1 receives the ball.
Example 2
Inputn = 5, k = 6
Output2
After 6 seconds of passing back and forth among 5 children, child 2 receives the ball.

Constraints

  • 2 <= n <= 50
  • 1 <= k <= 50

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate