Java ProgramsArraysRotate Array Right

Rotate Array Right in Java

beginner·  Arrays  ·  Array Manipulation

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.

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

Java Program

Java
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

[4, 5, 1, 2, 3]

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.

How It Works
  1. 1The outer loop runs k times, once per position to rotate.
  2. 2last saves arr[arr.length - 1] before it gets overwritten.
  3. 3An inner loop shifts every element one index to the right, walking backward: arr[i] = arr[i - 1].
  4. 4After the shift, last is placed at index 0, completing one full right rotation.
For [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.

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 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

[4, 5, 1, 2, 3]

Core Logic

Every element's final position after a right 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 + n) % n] computes, for each destination index i, exactly which original element lands there.
  3. 3The extra + n before the second % n keeps the index positive, since i - k can go negative in Java's % operator.
  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[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.

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 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

[4, 5, 1, 2, 3]

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 + n) % 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[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.

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