Array | Simple solution with 100% beaten
IntuitionTo solve this problem, we can simulate the movement of the ball. Initially, the ball starts at child 0, and it moves in the right direction. When the b
# Intuition
To solve this problem, we can simulate the movement of the ball. Initially, the ball starts at child 0, and it moves in the right direction. When the ball reaches either end, it reverses direction. We need to compute the final child after `k` seconds of this movement.

# Approach
1. Initialize the ball's position at child 0 (`pos = 0`).
2. Use a boolean flag (`direction`) to track whether the ball is moving right (`true`) or left (`false`).
3. For each second, update the position based on the current direction:
   - If moving right, increment the position. If the ball reaches the last child (position `n - 1`), reverse the direction.
   - If moving left, decrement the position. If the ball reaches the first child (position `0`), reverse the direction.
4. After `k` seconds, return the final position.

# Complexity
- Time complexity: $$O(k)$$, as we simulate the ball passing for `k` seconds.
- Space complexity: $$O(1)$$, as only a few variables are used to store the current position and direction.

# Code
```cpp
class Solution {
public:
    int numberOfChild(int n, int k) {
        int pos = 0; // Ball starts at child 0
        bool direction = true; // true = right, false = left

        for (int i = 0; i < k; ++i) {
            if (direction) {
                pos++; // Move right
                if (pos == n - 1) direction = false; // Reverse direction at right end
            } else {
                pos--; // Move left
                if (pos == 0) direction = true; // Reverse direction at left end
            }
        }
        return pos;
    }
};

['C++']