I was doing some basic problems to brush up my programming skills(its been 2 years). I was solving the question when an alternate method struck me.
1) When you rotate an array by, say 3 elements, the last n — 3 elements are directly moved up to the front. Keeping this in mind, I swapped the elements at 3 index difference.
2) This led to the last 3 elements not being in correct order, and actually offset by n % 3. So applying the same logic to them that was applied to the whole array, and repeating.
3) There will be exactly n — 1 operations (initial n — 3, then 1 (+ 1) to rearrange the last 3 elements), thus it is O(n), and memory of O(1).
Code:








