Find the Child Who Has the Ball After K Seconds - Problem
Imagine a line of n children standing in a queue, numbered from 0 to n-1 from left to right. Initially, child 0 holds a ball and starts passing it to the right.
Here's the twist: When the ball reaches either end of the line (child 0 or child n-1), the direction reverses! The ball bounces back and forth like a ping-pong ball.
Your task: Given n children and k seconds, determine which child will be holding the ball after exactly k seconds of passing.
Example: With 3 children [0,1,2], the ball moves: 0→1→2→1→0→1→2→...
Input & Output
example_1.py — Basic Case
$
Input:
n = 3, k = 5
›
Output:
1
💡 Note:
Ball movement: Start(0) → 1 → 2 → 1 → 0 → 1. After 5 seconds, ball is at position 1.
example_2.py — Single Child
$
Input:
n = 1, k = 1
›
Output:
0
💡 Note:
With only one child, the ball stays at position 0 regardless of time.
example_3.py — Large K Value
$
Input:
n = 4, k = 10
›
Output:
2
💡 Note:
Pattern: 0→1→2→3→2→1→0→1→2→3→2. After 10 seconds, ball is at position 2.
Constraints
- 2 ≤ n ≤ 100
- 1 ≤ k ≤ 109
- Time limit: 1 second per test case
Visualization
Tap to expand
Understanding the Visualization
1
Initial Pass
Ball starts at child 0 and moves right
2
Forward Journey
Ball travels from position 0 to n-1
3
Direction Reversal
Ball hits the end and bounces back
4
Return Journey
Ball travels from n-1 back to 0
5
Complete Cycle
Pattern repeats every 2×(n-1) seconds
Key Takeaway
🎯 Key Insight: The ball's movement is periodic with cycle length 2×(n-1). Use modular arithmetic to find the position directly without simulation!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code