💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nPython3 []\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> i
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/849f06d9-f2ec-40f2-a140-28ab88f17ceb_1717910223.636967.jpeg)\n![Screenshot 2024-06-09 104733.png](https://assets.leetcode.com/users/images/89d25cb7-9f9e-4baa-87ad-d13896b4740d_1717910264.5292177.png)\n![Screenshot 2024-06-09 104755.png](https://assets.leetcode.com/users/images/d9943e8e-37f5-4a1a-af91-b0d798c74bd6_1717910291.1037343.png)\n![Screenshot 2024-06-09 104821.png](https://assets.leetcode.com/users/images/bdda4eda-ab9b-49ff-8212-b0e931b7abc9_1717910313.949854.png)\n![Screenshot 2024-06-09 104632.png](https://assets.leetcode.com/users/images/d29f7d77-4c7f-4833-916a-085eeb086999_1717910231.3436897.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```Python3 []\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> int:\n        cycle_length = 2 * (n - 1)  # Length of one cycle\n        remainder = k % cycle_length  # Remainder after k seconds\n    \n        if remainder < n:  # Ball is within the range [0, n-1]\n            return remainder\n        else:  # Ball is within the range [n, 2 * (n - 1)]\n            return cycle_length - remainder\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int j = 0;\n        int d = 1;\n        while (k--) {\n            j += d;\n            if (j == n - 1 || j == 0)\n                d *= -1;\n        }\n        return j;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int numberOfChild(int n, int k) {\n        int j = 0, d = 1;\n        while (k-- != 0) {\n            j += d;\n            if (j == n - 1 || j == 0)\n                d *= -1;\n        }\n        return j;\n    }\n}\n```\n```Python []\nclass Solution(object):\n    def numberOfChild(self, n, k):\n        """\n        :type n: int\n        :type k: int\n        :rtype: int\n        """\n        c = 2 * (n -1)\n        r = k % c\n        return r if r < n else c - r\n```\n```C []\nint numberOfChild(int n, int k) {\n    int c = 2 * (n - 1);\n    int r = k % c;\n    return r < n ? r : c - r;\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     O(1)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> int:\n        cycle_length = 2 * (n - 1)  # Length of one cycle\n        remainder = k % cycle_length  # Remainder after k seconds\n    \n        if remainder < n:  # Ball is within the range [0, n-1]\n            return remainder\n        else:  # Ball is within the range [n, 2 * (n - 1)]\n            return cycle_length - remainder\n```\n# ***Please Upvote if it\'s useful for you***\n![th.jpeg](https://assets.leetcode.com/users/images/25a5a14f-645d-42f7-912c-b96cd7432275_1717910532.8266816.jpeg)\n
['Math', 'Brainteaser', 'C', 'Python', 'C++', 'Java', 'Python3']