Find a lesson, question or project

Start typing to search published content and planned structures.

↑ ↓ moveEnter openEsc close

Rotate an Array to the Left

Rotate an array by k positions in place with the reversal technique.

MediumArraysPattern: reversal

Problem statement

Rotate an array of length n to the left by k positions.

For [1, 2, 3, 4, 5] and k = 2, the result is [3, 4, 5, 1, 2].

Constraints & requirements

INPUT
An array and rotation count k
CHANGE
Rotate the same array
EDGE
Normalize k when it exceeds n

Examples 3 test cases

  1. Example 1 Normal rotation

    Input
    [1, 2, 3, 4, 5], k = 2
    Output
    [3, 4, 5, 1, 2]

    Checks all three reversals.

  2. Example 2 Large k

    Input
    [1, 2, 3], k = 5
    Output
    [3, 1, 2]

    Requires normalizing k by the length.

  3. Example 3 Full rotation

    Input
    [4, 8], k = 2
    Output
    [4, 8]

    Checks the no-change case.

Hints Optional

Hints become more specific one step at a time.
01Name the two blocks

For k = 2, think of the input as A = [1, 2] and B = [3, 4, 5]. The result is BA.

02Reverse both blocks

Reverse A, then reverse B. Their internal order is temporarily reversed.

03Reverse the whole array

One final full reversal restores each block’s order while swapping their positions.

How to think before coding

Write the desired result as two blocks: AB must become BA. A temporary output array makes the mapping obvious; repeated shifts also work but repeat movement. To avoid that storage, notice that reversing each block and then the whole array exchanges their positions while restoring internal order. Normalize k before choosing the three inclusive reversal ranges.

Brute force and the improvement

Using a temporary array is easier to derive and also takes O(n) time, but it needs O(n) extra space. Start with that solution if it helps you understand the index mapping, then learn the reversal method.

With normalized k, place original index i at (i - k + n) % n in a new array, then copy back. Repeated one-position shifts take O(nk) time and O(1) space. A cycle/juggling method also reaches O(n) time and O(1) space but requires careful cycle boundaries.

Intuition

Split the array into two parts: A = [1, 2] and B = [3, 4, 5]. The answer is BA.

We can build BA in place by reversing A, reversing B, and then reversing the entire array.

Java solution

Java
public static void rotateLeft(int[] values, int k) {
    // Stop before modulo n: an empty array would cause division by zero.
    if (values == null || values.length == 0) {
        return;
    }

    int n = values.length;
    k = ((k % n) + n) % n; // Remove full turns; negative k rotates right.

    // Reverse each block, then the whole array: AB becomes BA.
    reverse(values, 0, k - 1);
    reverse(values, k, n - 1);
    reverse(values, 0, n - 1);
}

private static void reverse(int[] values, int left, int right) {
    // Stop when pointers meet or cross; each swap fixes two outer positions.
    while (left < right) {
        int temporary = values[left]; // Preserve the value before overwriting it.
        values[left] = values[right];
        values[right] = temporary;
        left++; // Move both pointers toward the center.
        right--;
    }
}

Dry run

Numbered illustrated dry run for Rotate an Array to the Left; the text transcript explains the states.
Follow the numbered panels in order. Open full-size dry run ↗
Read the text transcript

For [1, 2, 3, 4, 5], k = 2, the blocks are A = [1, 2], B = [3, 4, 5].

Operation Array afterward Why
Normalize k [1, 2, 3, 4, 5] 2 % 5 is still 2
Reverse indexes 0..1 [2, 1, 3, 4, 5] Reverse A
Reverse indexes 2..4 [2, 1, 5, 4, 3] Reverse B
Reverse indexes 0..4 [3, 4, 5, 1, 2] Swap blocks and restore each block’s order

Within reverse, save the left value before overwriting it. For the final reversal, swaps (0,4) and (1,3) transform [2,1,5,4,3] into [3,4,5,1,2]; the pointers then meet at index 2.

Complexity

Time is O(n) and extra space is O(1).

Common mistakes

  • Forgetting to reduce k with modulo n
  • Dividing by zero when the array is empty
  • Mixing the steps for left rotation with the steps for right rotation

Practice next

Next lessonMove Zeroes to the End

Compare another constant-space transformation.

Back to Arrays →