How to Rotate an Array to the Right in Java
Right rotation is a common array operation where each element moves to the right by a specified number of positions. Elements that move past the end of the array wrap around and reappear at the beginning.
Right rotation is closely related to left rotation, but the algorithms are not identical. The index calculations and the order of operations in the Reversal Algorithm differ, making it important to understand the correct implementation.
In this tutorial, you'll learn multiple ways to rotate an array to the right, including an extra-array approach and the optimal in-place Reversal Algorithm.
Problem Statement
Given the following array:
int[] numbers = {1, 2, 3, 4, 5};
Rotate the array right by 2 positions.
Before Rotation
[1, 2, 3, 4, 5]
After Rotation
[4, 5, 1, 2, 3]
Left Rotation vs Right Rotation
Although both operations involve shifting elements, they move elements in opposite directions.
Left Rotation
Elements move toward the beginning of the array.
Original : [1, 2, 3, 4, 5]
Left by 2: [3, 4, 5, 1, 2]
Right Rotation
Elements move toward the end of the array.
Original : [1, 2, 3, 4, 5]
Right by 2: [4, 5, 1, 2, 3]
Although they appear symmetrical, the implementation details—especially for the Reversal Algorithm—are different.
Method 1: Using an Extra Array
A straightforward way to perform right rotation is to create another array and place each element into its new destination index.
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int k = 2;
int n = numbers.length;
k = k % n;
int[] rotated = new int[n];
for (int i = 0; i < n; i++) {
rotated[(i + k) % n] = numbers[i];
}
System.out.println(Arrays.toString(rotated));
}
}
Output
[4, 5, 1, 2, 3]
Explanation
For every element:
(i + k) % n
calculates its new destination index after the right rotation.
This approach is:
- Easy to understand
- Runs in O(n) time
- Uses O(n) extra space
- Preserves the original array
Method 2: Reversal Algorithm (Optimal)
The Reversal Algorithm performs right rotation without creating another array.
It rotates the array using three reversal operations.
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int k = 2;
int n = numbers.length;
k = k % n;
reverse(numbers, 0, n - 1);
reverse(numbers, 0, k - 1);
reverse(numbers, k, n - 1);
System.out.println(Arrays.toString(numbers));
}
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--;
}
}
}
Output
[4, 5, 1, 2, 3]
Explanation
The algorithm works in three steps:
- Reverse the entire array.
- Reverse the first k elements.
- Reverse the remaining elements.
Unlike left rotation, the entire array is reversed first, making this the key difference between the two algorithms.
Time Complexity: O(n)
Space Complexity: O(1)
Handling K Greater Than Array Length
Always reduce the rotation count before rotating.
Example
k = k % numbers.length;
Suppose:
Array Length = 5
k = 12
Then:
12 % 5 = 2
Rotating right by 12 positions produces exactly the same result as rotating right by 2 positions.
Normalizing k prevents unnecessary work and avoids incorrect index calculations.
Step-by-Step Explanation
Consider:
Array = [1, 2, 3, 4, 5]
k = 2
Step 1
Reverse the entire array.
[5, 4, 3, 2, 1]
Step 2
Reverse the first two elements.
[4, 5, 3, 2, 1]
Step 3
Reverse the remaining elements.
[4, 5, 1, 2, 3]
The array is now rotated right by two positions.
Internal Working
Original Array
[1, 2, 3, 4, 5]
Reverse Entire Array
[5, 4, 3, 2, 1]
Reverse First K Elements
[4, 5, 3, 2, 1]
Reverse Remaining Elements
[4, 5, 1, 2, 3]
Each reversal uses the same two-pointer swapping technique, resulting in an efficient in-place rotation.
Real-Life Analogy
Imagine five people standing in a queue.
A B C D E
If everyone moves two positions to the right, the last two people come to the front.
The new arrangement becomes:
D E A B C
The Reversal Algorithm performs this rearrangement without creating another queue.
Best Practices
- Always compute
k % nbefore rotating. - Use the Reversal Algorithm when memory efficiency is important.
- Use an extra array when you need to preserve the original array.
- Test edge cases such as
k = 0andk = n. - Keep the
reverse()method reusable.
Common Mistakes
1. Using the Left Rotation Reversal Order
Right rotation uses a different sequence of reversals.
Correct order:
- Reverse the entire array.
- Reverse the first k elements.
- Reverse the remaining elements.
2. Forgetting to Normalize K
Incorrect:
k = 17;
Correct:
k = k % numbers.length;
3. Mixing Up Source and Destination Indices
For right rotation using an extra array:
rotated[(i + k) % n] = numbers[i];
This calculates the destination index for each element.
4. Ignoring Edge Cases
Test:
k = 0k = n- Empty arrays
- Single-element arrays
All should work correctly.
Expert Tips
- A right rotation by k positions is equivalent to a left rotation by n − k positions.
- Understanding why the Reversal Algorithm works is more valuable than memorizing the steps.
- The same
reverse()helper method can be reused for left rotation, right rotation, and array reversal problems. - For applications requiring frequent rotations, a circular buffer may be a better choice than repeatedly rotating an array.
Comparison Table
| Method | Time Complexity | Space Complexity | In-Place? |
|---|---|---|---|
| Extra Array | O(n) | O(n) | ❌ No |
| Reversal Algorithm | O(n) | O(1) | ✅ Yes (Optimal) |
Frequently Asked Questions
1. How is right rotation different from left rotation?
Right rotation moves elements toward the end of the array, while left rotation moves elements toward the beginning. Their Reversal Algorithms use different sequences of reversals.
2. What is the correct order of reversals for right rotation?
The correct order is:
- Reverse the entire array.
- Reverse the first k elements.
- Reverse the remaining elements.
3. What happens if k is larger than the array length?
Always normalize the rotation count:
k = k % numbers.length;
Only the remainder determines the effective rotation.
4. Is rotating right by k the same as rotating left by n − k?
Yes. Both operations produce the same final arrangement.
5. Can I use an extra array for right rotation?
Yes. Calculate the destination index using:
(i + k) % n
and place each element accordingly.
6. What happens if k equals the array length?
The array remains unchanged because every element returns to its original position.
7. Can I rotate arrays of objects or strings?
Yes. Both the extra-array approach and the Reversal Algorithm work for arrays of any data type because they simply move elements or references.
8. Does Java provide a built-in method for array rotation?
Java provides Collections.rotate() for List objects, but there is no built-in method for rotating primitive arrays. For arrays, you must implement the rotation logic yourself.