[JavaScript] O(n) Time - simulate index
Idea is to keep moving 1 and check where number \'1\' is at any point of time. \n\nSimulate moving \'1\' leftwards/rightwards using below given observations til
Idea is to keep moving 1 and check where number \'1\' is at any point of time. \n\nSimulate moving \'1\' leftwards/rightwards using below given observations till it gets to the index 1.\n- any number (in our example \'1\') will move away from left end of the perm array 2 places, then 4 places.... till it gets to the second half of perm.\n- in second half, it will move away from the right end of the perm array 2 places, 4 places, 8 places........till it gets to the first half of perm.\n- Eventually it will be in its correct position i.e. 1 and every other number likewise will be.\n\n**E.g. n = 10**\n0,**1**,2,3,4,5,6,7,8,9\n0,5,**1**,6,2,7,3,8,4,9\n0,7,5,3,**1**,8,6,4,2,9\n0,8,7,6,5,4,3,2,**1**,9\n0,4,8,3,7,2,6,**1**,5,9\n0,2,4,6,8,**1**,3,5,7,9\n0,**1**,2,3,4,5,6,7,8,9\n\n```\nvar reinitializePermutation = function(n) {\n    \n    if(n == 2)\n        return 1;\n    \n    var ops = 1; // 1 because the perm starts with correct positions, so before entering the loop I am making one operation already    \n    var curr = 2;\n\t\n    var mid = n/2;\n    while(curr != 1){ // is not yet at index 1\n        if(curr < mid){ // is in first half\n            curr *= 2;\n        }\n        else{ // is in second half\n           curr = n - 1 - (n - 1 - curr)*2; \n        }\n        ops++;\n    }\n    \n    return ops;\n};\n```
[]