O(1) Solution | Beats 100💯
Number of Children in the Roundabout Game\n\n## Problem Statement\nGiven n children standing in a circle, you start counting them from 1 to k. Once you reach k,
# Number of Children in the Roundabout Game\n\n## Problem Statement\nGiven `n` children standing in a circle, you start counting them from 1 to `k`. Once you reach `k`, the count restarts from 1 again, but starting with the next child. After counting to `k` several times, determine which child will be the last one counted.\n\n## Intuition\nTo solve this problem, you can use the concept of rounds and remainders. Each complete round of counting `k` will cover all `n` children, and then the remainder will determine the final position.\n\n## Approach\n1. **Initialization**: Start by decrementing `n` by 1 to simplify calculations since we\'re dealing with 0-based indexing.\n2. **Rounds Calculation**: Determine the number of full rounds of `k` by integer division (`k / n`).\n3. **Remainder Calculation**: Find the remainder of `k` divided by `n` to get the position within the round.\n4. **Final Position Calculation**:\n    - If the number of full rounds is even, the position remains as the remainder.\n    - If the number of full rounds is odd, calculate the position from the other end of the circle.\n\n## Complexity\n- **Time complexity**: \\(O(1)\\), since the solution involves a constant number of arithmetic operations.\n- **Space complexity**: \\(O(1)\\), as no additional space is required other than a few integer variables.\n\n## Solution Code\n```cpp\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        n--; // Decrement n for 0-based indexing\n        int rounds = k / n; // Calculate full rounds of k\n        int rem = k % n; // Calculate remainder within the round\n\n        if (rounds % 2 == 0) {\n            return rem;\n        } else {\n            return (n - rem); \n        }\n    }\n};\n
['Math', 'C++']