# LC 3178. Find the Child Who Has the Ball After K Seconds ### [Problem link](https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/) ###### tags: `leedcode` `easy` `c++` You are given two **positive** integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> 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 <code>n - 1</code>, the direction of passing is **reversed** . Return the number of the child who receives the ball after <code>k</code> seconds. **Example 1:** <div class="example-block"> Input: <span class="example-io">n = 3, k = 5 Output: <span class="example-io">1 Explanation: <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[0, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, 1, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, 2]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2]</code></td> </tr> </tbody> </table> **Example 2:** <div class="example-block"> Input: <span class="example-io">n = 5, k = 6 Output: <span class="example-io">2 Explanation: <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, 2, 3, 4]</code></td> </tr> </tbody> </table> **Example 3:** <div class="example-block"> Input: <span class="example-io">n = 4, k = 2 Output: <span class="example-io">2 Explanation: <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[0, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, 1, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, 2, 3]</code></td> </tr> </tbody> </table> **Constraints:** - <code>2 <= n <= 50</code> - <code>1 <= k <= 50</code> ## Solution 1 #### C++ ```cpp= class Solution { public: int numberOfChild(int n, int k) { int q = k / (n - 1); int r = k % (n - 1); if (q % 2 == 1) { return n - 1 - r; } else { return r; } } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(1) | O(1) | ## Note [0x3F](https://youtu.be/PdNYcAwsCaw)