easy understanding solution beats 100% in time & space
Code\n\njavascript []\nvar numberOfChild = function (n, k) {\n  let ballPosition = 0; // Start with child 0 holding the ball\n  let direction = 1; // Initially,
# Code\n\n```javascript []\nvar numberOfChild = function (n, k) {\n  let ballPosition = 0; // Start with child 0 holding the ball\n  let direction = 1; // Initially, the ball moves towards the right\n\n  for (let i = 0; i < k; i++) {\n    if (direction === 1) {\n      if (ballPosition === n - 1) direction = -1; \n      ballPosition += direction;\n    } else {\n      if (ballPosition === 0) direction = 1;  \n      ballPosition += direction;\n    }\n  } \n  return ballPosition;\n};\n```\n```python []\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> int:\n        ball_position = 0  # Start with child 0 holding the ball\n        direction = 1  # Initially, the ball moves towards the right\n\n        for _ in range(k):\n            if direction == 1:\n                if ball_position == n - 1:\n                    direction = -1\n                ball_position += direction\n            else:\n                if ball_position == 0:\n                    direction = 1\n                ball_position += direction\n\n        return ball_position\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int ballPosition = 0; // Start with child 0 holding the ball\n        int direction = 1;    // Initially, the ball moves towards the right\n\n        for (int i = 0; i < k; i++) {\n            if (direction == 1) {\n                if (ballPosition == n - 1)\n                    direction = -1;\n                ballPosition += direction;\n            } else {\n                if (ballPosition == 0)\n                    direction = 1;\n                ballPosition += direction;\n            }\n        }\n        return ballPosition;\n    }\n};\n```\n```java []\nclass Solution {\n    public int numberOfChild(int n, int k) { \n        int ballPosition = 0; // Start with child 0 holding the ball\n        int direction = 1; // Initially, the ball moves towards the right\n\n        for (int i = 0; i < k; i++) {\n            if (direction == 1) {\n                if (ballPosition == n - 1) direction = -1; \n                ballPosition += direction;\n            } else {\n                if (ballPosition == 0) direction = 1;  \n                ballPosition += direction;\n            }\n        } \n        return ballPosition; \n    }\n}\n``` \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n
['C++', 'Java', 'Python3', 'JavaScript']