Rotate Array
Rotate Array is a medium arrays & hashing problem solved with the double reversal trick pattern.
The best approach, optimal (double reversal), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (extra array) up.
Problem
Rotate an array left by d positions, in place. Elements that fall off the front move to the back.
For a right rotation by k, rotate left by n - k % n, or reverse the whole array first.
Examples
Example 1
- Input
arr = [1, 2, 3, 4, 5, 6], d = 2- Output
[3, 4, 5, 6, 1, 2]- Why
- Rotated left by 2.
Example 2
- Input
arr = [7, 8, 9], d = 4- Output
[8, 9, 7]- Why
- Rotating by 4 is the same as rotating by 4 % 3 = 1.
Constraints
1 <= arr.length <= 10^50 <= d <= 10^9- Rotate in place.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (extra array) | O(n) | O(n) |
| Optimal (double reversal) | O(n) | O(1) |
1Brute force (extra array)
O(n)O(n)Copy each element to its new position (i - d) mod n in a new array, then copy back.
- d = d % n.
- tmp[i] = arr[(i + d) % n].
- Copy tmp back into arr.
class Solution {
static void rotateArr(int[] arr, int d) {
int n = arr.length;
d %= n;
int[] tmp = new int[n];
for (int i = 0; i < n; i++) tmp[i] = arr[(i + d) % n];
System.arraycopy(tmp, 0, arr, 0, n);
}
}class Solution:
def rotateArr(self, arr: List[int], d: int) -> None:
n = len(arr)
d %= n
tmp = [0] * n
for i in range(n):
tmp[i] = arr[(i + d) % n]
arr[:] = tmpclass Solution {
public:
void rotateArr(vector<int>& arr, int d) {
int n = arr.size();
d %= n;
vector<int> tmp(n);
for (int i = 0; i < n; i++) tmp[i] = arr[(i + d) % n];
arr = tmp;
}
};function rotateArr(arr, d) {
const n = arr.length;
d %= n;
const tmp = new Array(n);
for (let i = 0; i < n; i++) tmp[i] = arr[(i + d) % n];
for (let i = 0; i < n; i++) arr[i] = tmp[i];
}2Optimal (double reversal)
O(n)Each element is swapped at most twice.O(1)Reverse the first d elements, reverse the rest, then reverse the whole array. Each block ends up in its rotated place with its order restored.
- d = d % n.
- reverse(0, d - 1), reverse(d, n - 1), reverse(0, n - 1).
class Solution {
static void rotateArr(int[] arr, int d) {
int n = arr.length;
d %= n;
reverse(arr, 0, d - 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
}
private static void reverse(int[] a, int i, int j) {
while (i < j) {
int t = a[i]; a[i] = a[j]; a[j] = t;
i++; j--;
}
}
}class Solution:
def rotateArr(self, arr: List[int], d: int) -> None:
n = len(arr)
d %= n
self.reverse(arr, 0, d - 1)
self.reverse(arr, d, n - 1)
self.reverse(arr, 0, n - 1)
def reverse(self, a: List[int], i: int, j: int) -> None:
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1class Solution {
public:
void rotateArr(vector<int>& arr, int d) {
int n = arr.size();
d %= n;
reverse(arr, 0, d - 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
}
private:
void reverse(vector<int>& a, int i, int j) {
while (i < j) {
int t = a[i]; a[i] = a[j]; a[j] = t;
i++; j--;
}
}
};function rotateArr(arr, d) {
const n = arr.length;
d %= n;
reverse(arr, 0, d - 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
}
function reverse(a, i, j) {
while (i < j) {
const t = a[i]; a[i] = a[j]; a[j] = t;
i++; j--;
}
}Edge cases to test
- d larger than n (use d % n)
- d = 0 or d = n
Hints
Hint 1
What happens if you reverse the whole array, then reverse each part?
FAQ
What is the best time complexity for Rotate Array?
Optimal (double reversal) runs in O(n) time and O(1) extra space. Each element is swapped at most twice.
Which pattern does Rotate Array use?
It is a arrays & hashing problem that uses the double reversal trick pattern. Other problems with the same pattern: Reverse Words in a String.
Is there a brute force solution for Rotate Array?
Yes. Brute force (extra array) takes O(n) time and O(n) space. Copy each element to its new position (i - d) mod n in a new array, then copy back.
Which edge cases should I test for Rotate Array?
d larger than n (use d % n); d = 0 or d = n.