Easy C++ solution 0ms
Intuition\n Describe your first thoughts on how to solve this problem. The asks us that how many cycles of permutation will it take for the array to return to i
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->The asks us that how many cycles of permutation will it take for the array to return to its original state. So at first you might think that this question is just of implementaton but its not if you just directly implement this your time and space complexity will increase. \n# Approach\n<!-- Describe your approach to solving the problem. -->The optimized solution for this question is to do the whole permutation algorithm of just one number that is \'1\' as all numbers of the array return to there original places after some cycles so if 1 returns to its original place then it guaranteed that the other numbers have also returned to there original place so you return the number of cycles it took for \'1\' to return to his original place\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(1)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int reinitializePermutation(int n) {\n        vector<int> perm = {0,1};\n        vector<int> arr = {0,1};\n\n        arr[1] = (n / 2 + (arr[1] - 1) /  2);\n\n        int cnt = 1;\n\n        while(arr != perm){\n            if(arr[1] % 2 == 0){\n                arr[1] = arr[1] / 2;\n            }\n            else{\n            arr[1] = (n / 2 + (arr[1] - 1) /  2);\n            }\n            cnt++;\n        }\n        return cnt;\n    }\n};\n```
['C++']