๐ Introduction
Array rotation is a common problem in Data Structures and Algorithms (DSA), often asked in coding interviews. The task is simple: given an array, shift its elements by k positions either to the left or to the right.
For example
๐ Input: arr = [1, 2, 3, 4, 5], k = 2
๐ Left Rotation: [3, 4, 5, 1, 2]
๐ Right Rotation: [4, 5, 1, 2, 3]
This problem helps you understand array manipulation, modular arithmetic, and in-place algorithms.
๐งฉ Types of Array Rotation
There are mainly two types of rotation:
Left Rotation: Each element is shifted to the left by one, and the first element goes to the end.
Example:
[1, 2, 3, 4, 5]โ[2, 3, 4, 5, 1]
Right Rotation: Each element is shifted to the right by one, and the last element comes to the front.
Example:
[1, 2, 3, 4, 5]โ[5, 1, 2, 3, 4]
โ๏ธ Approaches to Solve the Problem
1๏ธโฃ Naive Approach (Using Extra Space)
Store elements of the array in a temporary array after rotation.
Copy them back to the original array.
โฑ๏ธ Time Complexity: O(n)
๐พ Space Complexity: O(n)
2๏ธโฃ Juggling Algorithm (Efficient)
Based on finding GCD (Greatest Common Divisor) of
nandk.Rotate elements in sets determined by GCD.
โฑ๏ธ Time Complexity: O(n)
๐พ Space Complexity: O(1)
3๏ธโฃ Reversal Algorithm (Most Popular)
For left rotation by k:
Reverse first
kelements.Reverse remaining
n-kelements.Reverse the whole array.
For right rotation by k:
Reverse last
kelements.Reverse first
n-kelements.Reverse the whole array.
โฑ๏ธ Time Complexity: O(n)
๐พ Space Complexity: O(1)
๐ป Code Implementations
โ C++ Code
#include <bits/stdc++.h>using namespace std;
void reverseArray(vector<int>& arr, int start, int end) {
while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}
void leftRotate(vector<int>& arr, int k) {
int n = arr.size();
k = k % n;
reverseArray(arr, 0, k-1);
reverseArray(arr, k, n-1);
reverseArray(arr, 0, n-1);
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
int k = 2;
leftRotate(arr, k);
for (int x : arr) cout << x << " ";
}โ Java Code
import java.util.*;
public class RotateArray {
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--;
}
}
static void leftRotate(int[] arr, int k) {
int n = arr.length;
k = k % n;
reverse(arr, 0, k-1);
reverse(arr, k, n-1);
reverse(arr, 0, n-1);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
leftRotate(arr, k);
System.out.println(Arrays.toString(arr));
}
}โ Python Code
def reverse(arr, start, end):
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
def left_rotate(arr, k):
n = len(arr)
k = k % n
reverse(arr, 0, k-1)
reverse(arr, k, n-1)
reverse(arr, 0, n-1)
return arr
arr = [1, 2, 3, 4, 5]
k = 2print(left_rotate(arr, k))๐ Time & Space Complexity
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Naive (Extra Space) | O(n) | O(n) |
| Juggling Algorithm | O(n) | O(1) |
| Reversal Algorithm | O(n) | O(1) |
๐ฏ Conclusion
Array rotation is a fundamental problem that strengthens your basics in DSA. Among different methods, the Reversal Algorithm is the most efficient and widely used in interviews.

Join the conversation! Your thoughts help the community grow.