Easy Video Solution 🔥 || How to 🤔 in Interview || O(N)->O(1) 🔥
If you like the solution Please Upvote and subscribe to my youtube channel\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nDry Run
***If you like the solution Please Upvote and subscribe to my youtube channel***\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDry Run few examples and observe how you can derive a formula\n\n\n\n# ***Easy Video Explanation***\n\nhttps://youtu.be/lTD4AqpPxUA\n\n    \n\n# Code (Brute Force)\n\n\n```C++ []\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int p = 0;\n        int f=0,i=0;\n        while(k>0) {\n            while(k>0 && i<n-1) {\n                i++;\n                k--;\n            }\n            if(k==0)\n                return i;\n            while(k>0 && i>0) {\n                i--;\n                k--;\n            }\n            if(k==0)\n                return i;\n        }\n        return 0;\n    }\n};\n```\n```python []\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> int:\n        i = 0\n        while k > 0:\n            while k > 0 and i < n - 1:\n                i += 1\n                k -= 1\n            if k == 0:\n                return i\n            while k > 0 and i > 0:\n                i -= 1\n                k -= 1\n            if k == 0:\n                return i\n        return 0\n\n```\n```Java []\n\nclass Solution {\n    public int numberOfChild(int n, int k) {\n        int i = 0;\n        while (k > 0) {\n            while (k > 0 && i < n - 1) {\n                i++;\n                k--;\n            }\n            if (k == 0) {\n                return i;\n            }\n            while (k > 0 && i > 0) {\n                i--;\n                k--;\n            }\n            if (k == 0) {\n                return i;\n            }\n        }\n        return 0;\n    }\n}\n\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(Max(N,k))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n\n# Code (Optimized)\n\n\n```C++ []\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        n--; // Decrement n to simplify calculation (so range is now 0 to n-1)\n        int rounds = k / n; // Calculate the number of complete back-and-forth trips\n        int rem = k % n; // Calculate the remaining steps after the last complete trip\n\n        if(rounds % 2 == 0) {\n            // If the number of complete back-and-forth trips is even\n            return rem; // The ball is passed forward from the start\n        } else {\n            // If the number of complete back-and-forth trips is odd\n            return (n - rem); // The ball is passed backward from the end\n        }\n    }\n};\n```\n```python []\nclass Solution:\n    def numberOfChild(self, n: int, k: int) -> int:\n        n -= 1 # Decrement n to simplify calculation (so range is now 0 to n-1)\n        rounds = k // n # Calculate the number of complete back-and-forth trips\n        rem = k % n # Calculate the remaining steps after the last complete trip\n\n        if rounds % 2 == 0:\n            # If the number of complete back-and-forth trips is even\n            return rem # The ball is passed forward from the start\n        else:\n            # If the number of complete back-and-forth trips is odd\n            return n - rem # The ball is passed backward from the end\n\n```\n```Java []\nclass Solution {\n    public int numberOfChild(int n, int k) {\n        n--; // Decrement n to simplify calculation (so range is now 0 to n-1)\n        int rounds = k / n; // Calculate the number of complete back-and-forth trips\n        int rem = k % n; // Calculate the remaining steps after the last complete trip\n\n        if (rounds % 2 == 0) {\n            // If the number of complete back-and-forth trips is even\n            return rem; // The ball is passed forward from the start\n        } else {\n            // If the number of complete back-and-forth trips is odd\n            return n - rem; // The ball is passed backward from the end\n        }\n    }\n}\n\n\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you* \uD83D\uDE00
['Math', 'Iterator', 'Python', 'C++', 'Java']