Rotate Array

Medium Arrays & Hashing Double Reversal Trick Original on GeeksforGeeks

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, 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^5
  • 0 <= d <= 10^9
  • Rotate in place.

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 Java code.

ApproachTimeSpace
Brute force (extra array)O(n)O(n)
Optimal (double reversal)O(n)O(1)

1Brute force (extra array)

TimeO(n)
SpaceO(n)

Copy each element to its new position (i - d) mod n in a new array, then copy back.

  1. d = d % n.
  2. tmp[i] = arr[(i + d) % n].
  3. Copy tmp back into arr.
Java
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);
    }
}

2Optimal (double reversal)

TimeO(n)Each element is swapped at most twice.
SpaceO(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.

  1. d = d % n.
  2. reverse(0, d - 1), reverse(d, n - 1), reverse(0, n - 1).
Java
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--;
        }
    }
}

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.