How to Rotate an Array to the Left in Java
Rotating an array is a common array manipulation problem in Java. In a left rotation, each element shifts to the left by a specified number of positions, and the elements removed from the beginning wrap around to the end of the array.
Array rotation is widely used in algorithmic problems and is a favorite topic in coding interviews because it introduces efficient in-place techniques such as the Reversal Algorithm, which achieves O(n) time complexity with O(1) extra space.
In this tutorial, you'll learn multiple ways to rotate an array to the left, understand how each approach works, and discover which method is best for different scenarios.
Problem Statement
Given the following array:
int[] numbers = {1, 2, 3, 4, 5};
Rotate the array left by 2 positions.
Before Rotation
[1, 2, 3, 4, 5]
After Rotation
[3, 4, 5, 1, 2]
Rotating by One Position (Building Block)
Before learning rotation by k positions, it's useful to understand how a single left rotation works.
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int first = numbers[0];
for (int i = 0; i < numbers.length - 1; i++) {
numbers[i] = numbers[i + 1];
}
numbers[numbers.length - 1] = first;
System.out.println(Arrays.toString(numbers));
}
}
Output
[2, 3, 4, 5, 1]
Explanation
- Store the first element.
- Shift every element one position to the left.
- Place the saved first element at the end.
To rotate by k positions, you could repeat this process k times, but this approach has a time complexity of O(n × k) and becomes inefficient for large values of k.
Method 1: Using an Extra Array
One of the simplest ways to rotate an array is by creating another array and copying each element to its new position.
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;
int[] rotated = new int[n];
for (int i = 0; i < n; i++) {
rotated[i] = numbers[(i + k) % n];
}
System.out.println(Arrays.toString(rotated));
}
}
Output
[3, 4, 5, 1, 2]
Explanation
The expression
(i + k) % n
calculates the correct position in the original array after rotating left.
Each element is copied exactly once, making this solution:
- Time Complexity: O(n)
- Space Complexity: O(n)
The original array remains unchanged.
Method 2: Reversal Algorithm (Optimal)
The Reversal Algorithm is the most efficient in-place solution for left rotation.
It performs the rotation using three reversals.
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, k - 1);
reverse(numbers, k, n - 1);
reverse(numbers, 0, 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
[3, 4, 5, 1, 2]
Explanation
The algorithm works in three steps:
- Reverse the first k elements.
- Reverse the remaining elements.
- Reverse the entire array.
This sequence produces the required left rotation while using only a single temporary variable.
Time Complexity: O(n)
Space Complexity: O(1)
Handling K Greater Than Array Length
If the rotation count is larger than the array size, only the remainder after division actually matters.
Example
k = k % numbers.length;
Suppose:
Array Length = 5
k = 12
Then:
12 % 5 = 2
Rotating by 12 positions produces exactly the same result as rotating by 2 positions.
Always normalize k before performing rotation.
Step-by-Step Explanation
Consider:
Array = [1, 2, 3, 4, 5]
k = 2
Step 1
Reverse the first two elements.
[2, 1, 3, 4, 5]
Step 2
Reverse the remaining elements.
[2, 1, 5, 4, 3]
Step 3
Reverse the complete array.
[3, 4, 5, 1, 2]
The array is now rotated left by two positions.
Internal Working
Original Array
[1, 2, 3, 4, 5]
Reverse First Part
[2, 1, 3, 4, 5]
Reverse Remaining Part
[2, 1, 5, 4, 3]
Reverse Entire Array
[3, 4, 5, 1, 2]
Each reversal uses the standard two-pointer swapping technique.
Although three reversals are performed, every element is processed only a constant number of times, resulting in an overall time complexity of O(n).
Real-Life Analogy
Imagine five people standing in a line.
A B C D E
If everyone moves two positions to the left, the first two people go to the back.
The new arrangement becomes:
C D E A B
The Reversal Algorithm achieves exactly this rearrangement without needing another line of people to temporarily hold anyone.
Best Practices
- Always compute
k % nbefore rotating. - Use the Reversal Algorithm when in-place rotation is required.
- Use an extra array when preserving the original array is important.
- Write a reusable
reverse()helper method. - Handle empty arrays before performing modulo operations.
Common Mistakes
1. Forgetting to Normalize K
Incorrect:
k = 12;
Correct:
k = k % numbers.length;
2. Performing the Reversals in the Wrong Order
The correct order is:
- Reverse first k elements.
- Reverse remaining elements.
- Reverse the complete array.
Changing this order produces incorrect results.
3. Rotating One Position K Times
Although correct, repeatedly rotating by one position results in:
Time Complexity = O(n × k)
which is much slower for large values of k.
4. Confusing Left Rotation with Right Rotation
Left rotation moves elements toward the beginning of the array.
Right rotation moves elements toward the end.
The algorithms are similar but use different reversal boundaries.
Expert Tips
- The Reversal Algorithm is one of the most frequently asked array interview questions.
- Understand why the three reversals work instead of simply memorizing them.
- The same
reverse()method can also be used to implement right rotation. - If rotations happen very frequently on huge datasets, consider using a circular buffer instead of repeatedly rotating an array.
Comparison Table
| Method | Time Complexity | Space Complexity | In-Place? |
|---|---|---|---|
| Rotate One Position Repeatedly | O(n × k) | O(1) | ✅ Yes |
| Extra Array | O(n) | O(n) | ❌ No |
| Reversal Algorithm | O(n) | O(1) | ✅ Yes (Optimal) |
Frequently Asked Questions
1. What is the most efficient way to rotate an array to the left?
The Reversal Algorithm is the most efficient approach because it runs in O(n) time while using O(1) extra space.
2. What happens if k is greater than the array length?
Compute:
k = k % numbers.length;
Only the remainder affects the final rotation.
3. Why does the Reversal Algorithm work?
By reversing the first segment, the second segment, and then the entire array, the two parts move into their correct rotated positions while preserving the relative order of elements.
4. When should I use an extra array?
Use an extra array when you want to keep the original array unchanged or when code simplicity is more important than memory usage.
5. Can the same technique be used for right rotation?
Yes. Right rotation also uses the Reversal Algorithm, but the reversal boundaries are different.
6. What happens if k is zero?
The array remains unchanged because no rotation is required.
7. What if k is negative?
A negative left rotation is generally treated as a right rotation. Normalize the value before applying the rotation.
8. Can I rotate an array of objects?
Yes. All rotation techniques work with object arrays as well because they simply move references rather than primitive values.