Rotate Array Right in Java
Problem
Rotating an array right by k positions moves the last k elements to the front, shifting every other element k places toward the back.
Given an array and a count k, rotate the array's elements to the right by k positions.
Java Program
import java.util.Arrays;
public class RotateArrayRight {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
for (int step = 0; step < k; step++) {
int last = arr[arr.length - 1]; // save it before it's overwritten
for (int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1]; // shift every element one slot right, walking backward
}
arr[0] = last; // the freed-up last element wraps to the front
}
System.out.println(Arrays.toString(arr));
}
}Output
Core Logic
Shifting every element one position to the right, and moving the freed-up last element to the front, repeated k times, is the mirror image of rotating left.
- 1The outer loop runs
ktimes, once per position to rotate. - 2
lastsavesarr[arr.length - 1]before it gets overwritten. - 3An inner loop shifts every element one index to the right, walking backward:
arr[i] = arr[i - 1]. - 4After the shift,
lastis placed at index0, completing one full right rotation.
[1, 2, 3, 4, 5] with k = 2, the first step produces [5, 1, 2, 3, 4], and the second step produces [4, 5, 1, 2, 3].Key Point: The inner loop has to walk backward, from the last index down to 1 — shifting forward instead would overwrite each element before it's been copied.
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 RotateArrayRightExtraArray {
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 + n) % n]; // + n corrects Java's negative-modulo result
}
System.out.println(Arrays.toString(rotated));
}
}
Output
Core Logic
Every element's final position after a right 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 + n) % n]computes, for each destination indexi, exactly which original element lands there. - 3The extra
+ nbefore the second% nkeeps the index positive, sincei - kcan go negative in Java's%operator. - 4One pass over all
nindices fills the entire rotated array.
[1, 2, 3, 4, 5] with k = 2, index 0 maps to arr[3] = 4, index 1 maps to arr[4] = 5, and so on, producing [4, 5, 1, 2, 3] in a single pass.Key Point: Java's % operator returns a negative result for a negative left-hand side, unlike some other languages — the + n correction is what keeps the index inside the valid array range.
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 RotateArrayRightStream {
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 + n) % 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 + n) % 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[3] = 4, 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 — including the + n correction for Java's negative-modulo behavior — 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.