C++ Easy Code💯 || Beats 100% || Beginner friendly || Easy to understand || With explanation 💯💀
Intuition\n Describe your first thoughts on how to solve this problem. \nThe numberOfChild function is designed to determine the position of a "child" in a sequ
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe numberOfChild function is designed to determine the position of a "child" in a sequence after k steps, given n total positions. Here\'s a step-by-step breakdown of the intuition behind the code:\n\nInitialization:\n\na represents the current position of the child, starting at position 0.\nb is a direction indicator, initialized to 1, meaning the child initially moves forward.\nMain Loop:\n\nThe loop runs k times, indicating the child will take k steps.\nIn each iteration, the child\'s position a is updated by adding b to it. This effectively moves the child one position forward if b is positive, or backward if b is negative.\nBoundary Checks:\n\nAfter updating a, the code checks if a has reached either end of the sequence (0 or n - 1).\nIf a is at one of the boundaries, the direction b is reversed (multiplied by -1). This causes the child to start moving in the opposite direction on the next step.\nReturn Value:\n\nAfter completing k steps, the function returns the final position a of the child.\nExample Walkthrough\nLet\'s walk through an example to understand how this works:\n\nSuppose n = 5 (positions 0 to 4) and k = 6 (6 steps).\nInitial state: a = 0, b = 1.\nSteps:\n\n1. a = 0 + 1 = 1\n2. a = 1 + 1 = 2\n3. a = 2 + 1 = 3\n4. a = 3 + 1 = 4 (boundary reached, reverse direction b = -1)\n5. a = 4 - 1 = 3\n6. a = 3 - 1 = 2\nAfter 6 steps, the child ends up at position 2.\n\nSummary\nThe numberOfChild function simulates a child moving back and forth in a sequence of positions, reversing direction each time a boundary is reached. This logic ensures that the child stays within the valid range of positions and allows for an easy calculation of the final position after a given number of steps.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of the numberOfChild function is O(k), where k is the number of steps the child takes. This is because the loop runs exactly k times, performing a constant amount of work in each iteration (updating the position and checking boundaries).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of the numberOfChild function is O(1). The function uses a fixed amount of additional space regardless of the input size. It only utilizes a few integer variables (a, b, i), which do not depend on the size of the input n or k.\n# Code\n```\nclass Solution {\npublic:\n    int numberOfChild(int n, int k) {\n        int a=0,b=1;\n       for(int i=0;i<k;i++){\n        a+=b;\n        if(a==0 || a==n-1){\n            b=-b;\n        }\n       }\n       return a;\n\n\n        \n    }\n};\n```
['C++']