Rotate Array
Rotate Array is LeetCode 189 (Medium). You are given an integer array nums and a number k. Rotate the array to the right by k steps, in place: each value moves k places to the right, and values that fall off the end wrap around to the front.
- In place: change
numsitself; the function returns nothing. - k can be larger than n:
kgoes up to 10⁵, while the array may be much shorter.
The follow-up asks for O(1) extra space, which rules out copying into a second array.
- 1 <= nums.length <= 10⁵
- -2³¹ <= nums[i] <= 2³¹ - 1
- 0 <= k <= 10⁵
Intuition
Rotating right by k cuts the array into two blocks, the first n − k values and the last k, and swaps the blocks while each block keeps its own order.
Reversing the whole array swaps the blocks, but it also turns each block backwards. Reversing each block once more turns it forwards again. So three reversals produce the rotation, and a reversal needs only two pointers and one swap at a time, so no extra array is used.
When a problem asks to move a block of an array to the other end in place, think of reversals: reversing the whole array swaps two blocks, and reversing each block restores its order. The same trick reverses the words of a string in place.
Approach
Before reading on, reverse [1, 2, 3, 4, 5, 6, 7] by hand, then reverse its first 3 values and its last 4. Compare the result with the array rotated right by 3.
Reduce k first
Rotating by n puts every value back where it started, so only the remainder counts: set k %= n. If k is now 0, there is nothing to do.
Reverse three times
Do the three reversals in this order:
- Whole array: the last
kvalues move to the front and the rest to the back, both backwards. - First k: turns the front block forwards again.
- Last n − k: turns the back block forwards again.
Reverse with two pointers
To reverse nums[lo..hi], swap nums[lo] and nums[hi], then move lo right and hi left, until they meet. Each swap puts two values in place, so a range of length m takes m / 2 swaps and no extra memory.
Code in Python, C++ and Java
5, 6, 7, end up at the front, in the same order. Three reversals do that in place.7, 6, 5, the right values in reverse order.[5, 6, 7, 1, 2, 3, 4] is the original array rotated right by 3. Every value was swapped at most twice, and no second array was used: O(n) time, O(1) extra space.3, 99, end up at the front, in the same order. Three reversals do that in place.99, 3, the right values in reverse order.[3, 99, -1, -100] is the original array rotated right by 2. Every value was swapped at most twice, and no second array was used: O(n) time, O(1) extra space.k % n matters: rotating by 3 is the same as rotating by 1. Without this step the split point 3 would be outside the array.2, the value that belongs there.[2, 1] is the original array rotated right by 1. Every value was swapped at most twice, and no second array was used: O(n) time, O(1) extra space.Common pitfalls
Forgetting k % n
reverse(0, n - 1) reverse(0, k - 1)
k %= n reverse(0, n - 1) reverse(0, k - 1)
With nums = [1, 2] and k = 3, reverse(0, 2) reaches index 2, which does not exist: Python raises IndexError, and C++ reads and writes outside the array.
Splitting at n − k after the full reversal
reverse(0, n - k - 1) reverse(n - k, n - 1)
reverse(0, k - 1) reverse(k, n - 1)
After the whole array is reversed, the k values that belong at the front occupy the first k places. Splitting at n − k reverses the wrong blocks, and the array is rotated by the wrong amount.
Complexity
Rotate Array FAQ
How do you write it in Python with slicing?
nums[:] = nums[-k:] + nums[:-k] after k %= len(nums). It is correct and short, but it builds a new list of n values, so it uses O(n) extra space instead of O(1). Write nums[:] =, not nums =: LeetCode checks the original list, and plain assignment only rebinds the local name.
How do you rotate an array to the left instead?
Swap the order of the reversals: reverse the first k, reverse the last n − k, then reverse the whole array. The GeeksforGeeks version of this problem rotates left (counter-clockwise), so check the direction before using this code.
Is there another O(1) space way?
Yes, cyclic replacement: move the value at index 0 to index k, the value found there to 2k, and so on, wrapping with % n, until the cycle returns to its start; then begin a new cycle at index 1. There are gcd(n, k) cycles. It is also O(n) time, but keeping count of cycles is easy to get wrong, so the three reversals are the usual answer.