Rotate Array Left in Java
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.
Java Program
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
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.
- 1The outer loop runs
ktimes, once per position to rotate. - 2
firstsavesarr[0]before it gets overwritten. - 3An inner loop shifts every element one index to the left:
arr[i] = arr[i + 1]. - 4After the shift,
firstis placed at the last index, completing one full left rotation.
[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.
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
Approach 2: Extra Array (Modulo Indexing)
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
Core Logic
Every element's final position after a left rotation can be computed directly with modulo arithmetic, without simulating each intermediate step.
- 1A new array
rotatedthe same size asarris created to hold the result. - 2
rotated[i] = arr[(i + k) % n]computes, for each destination indexi, exactly which original element lands there. - 3The modulo wraps the index back around once
i + kpasses the end of the array. - 4One pass over all
nindices fills the entire rotated array.
[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.
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
Approach 3: Java 8
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
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.
- 1
IntStream.range(0, n)generates every valid destination index. - 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
.toArray()collects the mapped values into a brand-newint[].
[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.
Why: The stream maps every destination index once and toArray() builds a new array holding all n rotated elements.