[Java] Greedy Initiation, O(1) space
Think greedy, manually work for example n=6 and see pattern, nums in 1st half goes in odd positions(even indeces) and nums in 2nd half goes in even positions(od
Think greedy, manually work for example n=6 and see pattern, nums in 1st half goes in odd positions(even indeces) and nums in 2nd half goes in even positions(odd indeces) in order, so if any number comes back to it original position then all nums attain original positions so simulate for any one number btw 1 to n-2 bcz 0, n-1 will never move from its places,in above implementation for ind=1 is simulated.\n```\nfor n=6;\nInitial permutation --> [0,1,2,3,4,5]\n\t\t\t\t\t -> [0,3,1,4,2,5]\n                     -> [0,4,3,2,1,5]\n                     -> [0,2,4,1,3,5]\n                     -> [0,1,2,3,4,5]\n```\n<h3>Code</h3>\n\n```\nclass Solution {\n    public int reinitializePermutation(int n) {\n        int res=0;\n        int ind=1;\n        do{\n            if(ind<n/2)\n                ind*=2;\n            else{\n                ind=(ind-n/2)*2+1;\n            }\n            res++;\n        }while(ind!=1);\n        return res;\n    }\n}\n```
[]