One Line Solution: Time : O(1) & Space : O(1)
Intuition\nThe problem can be thought of as simulating the movement of the ball between the children while alternating directions when it reaches either end. In
# Intuition\nThe problem can be thought of as simulating the movement of the ball between the children while alternating directions when it reaches either end. Instead of simulating every step explicitly, the key is recognizing the repetitive nature of the sequence.\n\nThe ball oscillates between the children in a predictable pattern, forming a "bounce-back" mechanism at the two ends (0 and n-1).\nUsing this insight, you can calculate the final position mathematically by reducing unnecessary calculations.\n\n**Example Walkthrough**\n```\nInput: n = 3, k = 5\nStep-by-step Calculation:\n\nThe total cycle length is 2 * (n - 1) = 4.\nk = 5 \u2192 Remainder when 5 is divided by 4 is 1.\nSince (5 / (n - 1)) % 2 == 1, the ball is in the "backward" phase.\nFinal position: (n - 1) - (k % (n - 1)) = 2 - (5 % 2) = 1.\n```\n\n# Approach\n**1. Observation of the Ball Movement:**\nEach complete traversal from 0 to n-1 and back takes 2 * (n-1) steps.\nThe position of the ball after k seconds depends on the remainder of k when divided by 2 * (n-1).\n\n**2. Handling Direction Reversal:**\nIf the ball moves to the right and reverses direction, the position in the second half of the cycle can be derived by subtracting from the last position.\n\n**3 Mathematical Computation:**\nDetermine which phase of the traversal the ball is in (moving forward or backward) using (k / (n-1)) % 2:\n\n- 0: Ball is moving forward.\n- 1: Ball is moving backward.\n\nCalculate the final position using modular arithmetic:\n- If moving forward: k % (n-1).\n- If moving backward: (n-1) - (k % (n-1)).\n\n**4. Implementation:** Using simple modular arithmetic, you can determine the final position of the ball after k seconds without simulating all steps.\n\n\n# Complexity\n- Time complexity: O(1)\n\n- Space complexity: O(1)\n\n# Code\n```java []\nclass Solution {\n    public int numberOfChild(int n, int k) {\n          \n        return (k / (n-1)) % 2 == 0 ? k % (n-1) : (n-1) - (k % (n-1));\n\n    }\n}\n\n\n```
['Java']