Java ProgramsArraysRotate Array Left

Rotate Array Left in Java

beginner·  Arrays  ·  Array Manipulation

Problem

Rotating an array left by k positions moves the first k elements to the end, shifting every other element k places toward the front.

Given an array and a count k, rotate the array's elements to the left by k positions.

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

Java Program

Java
import java.util.Arrays; public class RotateArrayLeft { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; for (int step = 0; step < k; step++) { int first = arr[0]; // save it before it's overwritten for (int i = 0; i < arr.length - 1; i++) { arr[i] = arr[i + 1]; // shift every element one slot left } arr[arr.length - 1] = first; // the freed-up first element wraps to the end } System.out.println(Arrays.toString(arr)); } }

Output

[3, 4, 5, 1, 2]

Core Logic

Shifting every element one position to the left, and moving the freed-up first element to the end, repeated k times, walks the array through each rotation step explicitly.

How It Works
  1. 1The outer loop runs k times, once per position to rotate.
  2. 2first saves arr[0] before it gets overwritten.
  3. 3An inner loop shifts every element one index to the left: arr[i] = arr[i + 1].
  4. 4After the shift, first is placed at the last index, completing one full left rotation.
For [1, 2, 3, 4, 5] with k = 2, the first step produces [2, 3, 4, 5, 1], and the second step produces [3, 4, 5, 1, 2].
💡

Key Point: Repeating a single-step shift k times is easy to follow, but it re-shifts the whole array on every step — for a large k, computing each position's final destination directly is far more efficient.

Complexity
Time Complexity: O(n × k)Space Complexity: O(1)

Why: Each of the k rotation steps shifts every one of the n elements by one position, so the total work multiplies across steps.

Key Concepts

nested for loopsingle-step shiftwraparound

Approach 2: Extra Array (Modulo Indexing)

Java
import java.util.Arrays; public class RotateArrayLeftExtraArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; int n = arr.length; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[i] = arr[(i + k) % n]; // maps destination index i to its source element } System.out.println(Arrays.toString(rotated)); } }

Output

[3, 4, 5, 1, 2]

Core Logic

Every element's final position after a left rotation can be computed directly with modulo arithmetic, without simulating each intermediate step.

How It Works
  1. 1A new array rotated the same size as arr is created to hold the result.
  2. 2rotated[i] = arr[(i + k) % n] computes, for each destination index i, exactly which original element lands there.
  3. 3The modulo wraps the index back around once i + k passes the end of the array.
  4. 4One pass over all n indices fills the entire rotated array.
For [1, 2, 3, 4, 5] with k = 2, index 0 maps to arr[2] = 3, index 1 maps to arr[3] = 4, and so on, producing [3, 4, 5, 1, 2] in a single pass.
💡

Key Point: This computes the whole rotation in one O(n) pass regardless of how large k is, unlike the step-by-step version whose cost grows with k.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each position is computed directly via modulo arithmetic in one pass, at the cost of a brand-new array the same size as the original.

Key Concepts

modulo arithmeticextra array

Approach 3: Java 8

Java
import java.util.Arrays; import java.util.stream.IntStream; public class RotateArrayLeftStream { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; int n = arr.length; // Maps each destination index to its source element via the same modulo formula int[] rotated = IntStream.range(0, n) .map(i -> arr[(i + k) % n]) .toArray(); System.out.println(Arrays.toString(rotated)); } }

Output

[3, 4, 5, 1, 2]

Core Logic

The same modulo-index mapping can be expressed as a stream — map each destination index to its source element and collect the results into a new array.

How It Works
  1. 1IntStream.range(0, n) generates every valid destination index.
  2. 2.map(i -> arr[(i + k) % n]) maps each destination index to the element that lands there, the same formula the extra-array version uses.
  3. 3.toArray() collects the mapped values into a brand-new int[].
For [1, 2, 3, 4, 5] with k = 2, index 0 maps to arr[2] = 3, the same first element the extra-array version computes.
💡

Key Point: This is the same O(n) modulo-indexing technique as the extra-array approach, just expressed as a stream pipeline instead of a manual loop.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The stream maps every destination index once and toArray() builds a new array holding all n rotated elements.

Key Concepts

StreamIntStreammodulo arithmetic

Related Programs