Java ProgramsArraysRotate Array by K Positions

Rotate Array by K Positions in Java

intermediate·  Arrays  ·  Array Manipulation

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.

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

Java Program

Java
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

[3, 4, 5, 1, 2]

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.

How It Works
  1. 1k = 7 % arr.length normalizes 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. 2reverse(arr, 0, k - 1) reverses just the first k elements in place.
  3. 3reverse(arr, k, arr.length - 1) reverses the remaining elements in place.
  4. 4reverse(arr, 0, arr.length - 1) reverses the entire array, which untangles the two separately-reversed halves into the correctly rotated order.
For [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.

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

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

reversal algorithmin-place swapk normalization

Approach 2: Java 8

Java
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

[3, 4, 5, 1, 2]

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.

How It Works
  1. 1k = 7 % arr.length normalizes k exactly as the in-place version does.
  2. 2IntStream.range(0, n) generates every destination index.
  3. 3.map(i -> arr[(i + k) % n]) maps each index to the element that belongs there after rotation.
  4. 4.toArray() collects the mapped values into a brand-new rotated array.
For [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.

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

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.

Key Concepts

StreamIntStreammodulo arithmetic

Related Programs