Rotate Array by K Positions in Java
Problem
The reversal algorithm rotates an array in place by reversing three specific stretches of it, without ever shifting elements one step at a time or allocating a second array.
Given an array and a count k, rotate the array left by k positions in place, in linear time and constant extra space.
Java Program
import java.util.Arrays;
public class RotateArrayByK {
static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 7 % arr.length; // normalize k so it never exceeds the array length
reverse(arr, 0, k - 1); // reverse the first k elements
reverse(arr, k, arr.length - 1); // reverse the remaining elements
reverse(arr, 0, arr.length - 1); // reverse the whole array to untangle both halves
System.out.println(Arrays.toString(arr));
}
}Output
Core Logic
Reversing the first k elements, then the remaining elements, then the whole array, produces exactly the same result as a left rotation — with no extra array and no repeated single-step shifting.
- 1
k = 7 % arr.lengthnormalizes k first, since rotating by more than the array's length just wraps around and repeats — a k of 7 on a 5-element array behaves exactly like a k of 2. - 2
reverse(arr, 0, k - 1)reverses just the first k elements in place. - 3
reverse(arr, k, arr.length - 1)reverses the remaining elements in place. - 4
reverse(arr, 0, arr.length - 1)reverses the entire array, which untangles the two separately-reversed halves into the correctly rotated order.
[1, 2, 3, 4, 5] with k = 2 (after normalizing 7), reversing the first two gives [2, 1, 3, 4, 5], reversing the rest gives [2, 1, 5, 4, 3], and reversing the whole thing gives [3, 4, 5, 1, 2].Key Point: This is the same in-place two-pointer reverse() helper used elsewhere for reversing an array — rotation here is built entirely out of that one simpler operation, applied three times.
Why: Each of the three reversal passes visits its portion of the array once, and no extra array is allocated — the whole rotation happens by swapping in place.
Key Concepts
Approach 2: Java 8
import java.util.Arrays;
import java.util.stream.IntStream;
public class RotateArrayByKStream {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
int k = 7 % n;
// Maps each destination index to its source element after rotation
int[] rotated = IntStream.range(0, n)
.map(i -> arr[(i + k) % n])
.toArray();
System.out.println(Arrays.toString(rotated));
}
}
Output
Core Logic
The same normalized k can drive a stream that maps each destination index to its source element directly, the declarative counterpart to the in-place reversal trick.
- 1
k = 7 % arr.lengthnormalizes k exactly as the in-place version does. - 2
IntStream.range(0, n)generates every destination index. - 3
.map(i -> arr[(i + k) % n])maps each index to the element that belongs there after rotation. - 4
.toArray()collects the mapped values into a brand-new rotated array.
[1, 2, 3, 4, 5] with k = 2, index 0 maps to arr[2] = 3, producing the same [3, 4, 5, 1, 2] as the reversal algorithm.Key Point: This trades the reversal algorithm's O(1) in-place space for a more declarative expression that builds a brand-new array instead.
Why: The stream maps every index through modulo arithmetic in one pass, but builds a brand-new array to hold the result instead of rotating in place.