✅✅💯EASY SOLUTION || C++ 🔥🔥💯✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \nImagine children playing a game of "pass the ball" in a line.\nThe ball starts with the
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine children playing a game of "pass the ball" in a line.\nThe ball starts with the first child and moves one step right each second.\nWhen the ball reaches the last child, they throw it back (reverse direction).\nWhen the ball reaches the first child again, they throw it forward (reverse direction again).\nThe ball keeps moving like this: right, right, ..., reverse, left, left, ..., reverse, right, and so on.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStart with position = 0 (first child) and direction = 1 (moving right).\nFor each of the k seconds:\nMove the ball: position += direction\n\nIf direction is 1, move right. If -1, move left.\nCheck if we need to reverse direction:\nIf position is n-1 (last child), set direction = -1 (start moving left).\nIf position is 0 (first child), set direction = 1 (start moving right).\n\n\n\n\nAfter k seconds, return the final position.\n# Complexity\n- Time complexity:O(k) - Depends on the number of seconds.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int position = 0;\n    int direction = 1;\n    \n    for (int i = 0; i < k; ++i) {\n        position += direction;\n        \n        // Reverse direction if the ball reaches either end\n        if (position == n - 1) {\n            direction = -1;\n        } else if (position == 0) {\n            direction = 1;\n        }\n    }\n    \n    return position;\n    }\n};\n```
['C++']