There are n children numbered from 0 to n - 1 standing in a line from left to right. At time 0, child 0 holds a ball and the passing direction is to the right. Every second, the child holding the ball passes it to the next child in the current direction. When the ball reaches either end of the line (child 0 or child n - 1), the passing direction reverses.
Given two positive integers n and k, return the number of the child who holds the ball after k seconds.
Function Signature
def find_child_with_ball(n: int, k: int) -> int:
Rules
-
At time 0 the ball is with child
0
, and each second moves it exactly one position.
-
The direction flips as soon as the ball arrives at child
0
or child
n - 1
, so the next pass heads back toward the other end.
-
When
n = 1
there is no neighbor to pass to, so the only child keeps the ball and the answer is
0
.
Constraints
-
1 <= n <= 50
-
1 <= k <= 50
Examples
Input: n = 3, k = 5
Output: 1
The ball is with children 0, 1, 2, 1, 0, 1 at times 0 through 5, so child 1 holds it at time 5.
Input: n = 5, k = 6
Output: 2
The ball moves right from child 0 to child 4 by time 4, then turns around and reaches child 3 at time 5 and child 2 at time 6.
Input: n = 1, k = 4
Output: 0
With a single child there is nobody to pass to, so child 0 holds the ball the whole time.