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.
Example 1:
Input: n = 3, k = 5
Output: 1
Explanation:
Time elapsed | Children |
---|---|
0 |
[0, 1, 2] |
1 |
[0, 1, 2] |
2 |
[0, 1, 2] |
3 |
[0, 1, 2] |
4 |
[0, 1, 2] |
5 |
[0, 1, 2] |
Example 2:
Input: n = 5, k = 6
Output: 2
Explanation:
Time elapsed | Children |
---|---|
0 |
[0, 1, 2, 3, 4] |
1 |
[0, 1, 2, 3, 4] |
2 |
[0, 1, 2, 3, 4] |
3 |
[0, 1, 2, 3, 4] |
4 |
[0, 1, 2, 3, 4] |
5 |
[0, 1, 2, 3, 4] |
6 |
[0, 1, 2, 3, 4] |
Example 3:
Input: n = 4, k = 2
Output: 2
Explanation:
Time elapsed | Children |
---|---|
0 |
[0, 1, 2, 3] |
1 |
[0, 1, 2, 3] |
2 |
[0, 1, 2, 3] |
Constraints:
2 <= n <= 50
1 <= k <= 50
Note: This question is the same as 2582: Pass the Pillow.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're tracking a ball being passed between children. The brute force method simulates the ball's journey step-by-step, mimicking each pass until the desired time is reached. We'll follow the ball as it moves, noting who has it at each second.
Here's how the algorithm would work step-by-step:
def find_child_with_ball_brute_force(
number_of_children,
initial_child,
passing_time
):
current_child = initial_child
# Simulate each second of passing
for current_second in range(passing_time):
# Determine the next child based on who has the ball now
if current_child % 2 == 0:
next_child = (current_child + 1) % number_of_children
# Odd numbered child passes to the previous child
else:
next_child = (current_child - 1) % number_of_children
current_child = next_child
return current_child
The problem involves figuring out where a ball ends up after being passed around a circle of children for a certain amount of time. The most efficient way to solve it involves using the remainder after dividing the total time by the number of children to quickly find the final child.
Here's how the algorithm would work step-by-step:
def find_the_child_with_ball(number_of_children, start_child, time_passed):
# Calculate the remainder to find the effective number of passes.
remainder = time_passed % number_of_children
final_child = start_child + remainder
# Adjust if the final child exceeds the number of children.
if final_child > number_of_children:
final_child = final_child - number_of_children
# Return the child number holding the ball.
return final_child
Case | How to Handle |
---|---|
Null or empty array for child positions | Return -1 immediately, indicating no child has the ball. |
K is zero | Return the initial position, as no time has passed. |
Array with a single child | The ball remains with this single child regardless of K, so return that position. |
Ball returns to the starting position before K seconds elapse. | Handle using the modulo operator (%) to account for cyclical movement. |
K is a very large number (potential overflow if not handled correctly) | Use modulo operator to reduce K to its effective range based on array length to prevent integer overflow. |
Children positions are not sequential (e.g., 1, 3, 5) | The algorithm should correctly handle jumps between children, not assuming sequential numbering. |
Negative K value | Return the initial position or treat K as its absolute value, documenting chosen behavior clearly. |
Array contains duplicate child indices. | Return -1 or throw an exception as this is invalid input. |