gimpey | JS | O(log(n)) Solution | Beats 100%
Statement\n\nThe problem can be solved using O(log(n)) time complexity and O(1) space complexity.\n\n# How To\n\nWe track the index of the first element until i
# Statement\n\nThe problem can be solved using $$O(log(n))$$ time complexity and $$O(1)$$ space complexity.\n\n# How To\n\nWe track the index of the first element until it returns to its initial state. This tracking is performed by $$Mod(2i,n-1)$$ where $$i$$ represents the previous index. Once the first index returns to the first position, we know the cycle is complete.\n\n# Code\n\nCheck out the code below:\n\n```\nvar reinitializePermutation = function(n) {\n    let count = 0;\n    let index = 1;\n    \n    while (count === 0 || index > 1) {\n        index = (index * 2) % (n - 1);\n        count++;\n    }\n    \n    return count;\n};\n\n```
['JavaScript']