🔥Very Easy || 4 lines || Without Formula || Changing direction at the ends.
Complexity\n- Time complexity:\nO(k) \n- Space complexity:\nO(1) \n\n# Code\n\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int dir
# Complexity\n- Time complexity:\n$$O(k)$$ \n- Space complexity:\n$$O(1)$$ \n\n# Code\n```\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int direction = 1; // 1 means right, -1 means left\n        int position = 0;  // Starting position\n\n        for (int steps = 0; steps < k; ++steps) {\n            position += direction;\n\n            // Change direction if at either end\n            if (position == 0 || position == n - 1) {\n                direction = -direction;\n            }\n        }\n        return position;\n    }\n};\n```
['C++']