beginnerArray ProblemsJava

Rotate an Array to the Left by One Position

Rotate an integer array one position to the left in place.

Quick Answer

Save the first element in a temporary variable, shift every other element one index to the left (arr[i] = arr[i + 1]), then place the saved value into the last slot. This left-rotates the array by one in O(n) time and O(1) extra space.

Problem Statement

Given an integer array, rotate it to the left by one position. Every element moves one index lower, and the element that was first wraps around to become the last. Do it in place, without allocating a second array.

For example, [1, 2, 3, 4, 5] becomes [2, 3, 4, 5, 1].

Input: An integer array arr of length n (n >= 1).

Output: The same array, rotated left by one position.

Examples

Example 1
Input:  [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]

Each element moves left one slot; the leading 1 wraps to the end.

Example 2
Input:  [7, 8, 9]
Output: [8, 9, 7]

8 and 9 shift left, and 7 moves to the last position.

Constraints

  • 1 <= n <= 10^6
  • Must be done in place using O(1) extra space

Think Before You Code

Reveal the questions to ask yourself first
  • If you overwrite arr[0] first, how will you ever recover the value it held?
  • In which direction should the copy loop run so you never lose a value you still need?
  • Where does the element you saved at the start belong when the shifting is done?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
The first element has to go somewhere, so rescue it into a temporary variable before you overwrite anything.
Hint 2
Shift left with `arr[i] = arr[i + 1]` for i from 0 up to n - 2; each element pulls in its right neighbour.
Hint 3
After the loop, drop the saved first value into `arr[n - 1]` to complete the wrap-around.

Approach

Reveal the step-by-step approach

Rotate left by one with a single temporary variable.

  1. If the array has fewer than two elements, there is nothing to rotate.
  2. Save first = arr[0].
  3. For i from 0 to n - 2, set arr[i] = arr[i + 1] so every element slides one place to the left.
  4. Set arr[n - 1] = first to wrap the original first element to the end.

Running the copy loop left to right is safe because each arr[i] is written only after its old value has already been shifted away.

Dry Run

Walk through the example step by step

Rotating [1, 2, 3, 4, 5] left by one, with first = 1:

step             | array
-----------------+------------------
save first = 1   | [1, 2, 3, 4, 5]
arr[0]=arr[1]    | [2, 2, 3, 4, 5]
arr[1]=arr[2]    | [2, 3, 3, 4, 5]
arr[2]=arr[3]    | [2, 3, 4, 4, 5]
arr[3]=arr[4]    | [2, 3, 4, 5, 5]
arr[4]=first     | [2, 3, 4, 5, 1]

Solution

Reveal the full Java solution
import java.util.Arrays;

public class RotateLeftByOne {
    public static void rotateLeft(int[] arr) {
        int n = arr.length;
        if (n < 2) {
            return;
        }
        int first = arr[0];
        for (int i = 0; i < n - 1; i++) {
            arr[i] = arr[i + 1];
        }
        arr[n - 1] = first;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        rotateLeft(a);
        System.out.println(Arrays.toString(a)); // [2, 3, 4, 5, 1]

        int[] b = {7, 8, 9};
        rotateLeft(b);
        System.out.println(Arrays.toString(b)); // [8, 9, 7]
    }
}

Only one value — the original first element — is ever at risk of being lost, so a single temporary variable is enough. The shift touches each of the n elements once, giving O(n) time, and the temporary variable is the only extra memory, giving O(1) space. Guarding for n < 2 keeps empty and single-element arrays safe.

Time: O(n)Space: O(1)

Common Mistakes

  • Overwriting arr[0] before saving it, which permanently loses the first element.
  • Forgetting to place the saved value into the last slot, leaving a duplicated element.

Edge Cases to Test

  • A single-element array is unchanged by a one-position rotation.
  • An empty array must not throw; guard the length before indexing.
  • An array with duplicate values still rotates correctly since positions, not values, move.

Interview Follow-Ups

  • How would you rotate left by k positions efficiently for large k?
  • How does the reversal algorithm rotate an array in O(n) time and O(1) space?
  • How would you rotate the array to the right by one instead?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us