04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Search...
DSA Course DSA Interview Questions on Array Practice Array MCQs on Array Tutorial on Array
Rotate an Array by d - Counterclockwise or Left
Last Updated : 23 Jul, 2025
Given an array of integers arr[] of size n, the task is to rotate the array
elements to the left by d positions.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, d = 2
Output: {3, 4, 5, 6, 1, 2}
Explanation: After first left rotation, arr[] becomes {2, 3, 4, 5, 6, 1}
and after the second rotation, arr[] becomes {3, 4, 5, 6, 1, 2}
Input: arr[] = {1, 2, 3}, d = 4
Output: {2, 3, 1}
Explanation: The array is rotated as follows:
After first left rotation, arr[] = {2, 3, 1}
After second left rotation, arr[] = {3, 1, 2}
After third left rotation, arr[] = {1, 2, 3}
After fourth left rotation, arr[] = {2, 3, 1}
Table of Content
[Naive Approach] Rotate one by one - O(n * d) Time and O(1)
Space
[Better Approach] Using Temporary Array - O(n) Time and O(n)
Space
[Expected Approach 1] Using Juggling Algorithm - O(n) Time and
O(1) Space
[Expected Approach 2] Using Reversal Algorithm - O(n) Time
and O(1) Space
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
https://www.geeksforgeeks.org/dsa/array-rotation/ 1/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
[Naive Approach] Rotate one by one - O(n * d) Time and O(1)
Space
In each iteration, shift the elements by one position to the left in a
circular fashion (the first element becomes the last). Perform this
operation d times to rotate the elements to the left by d positions.
Illustration:
Let us take arr[] = {1, 2, 3, 4, 5, 6}, d = 2.
First Step:
=> Rotate to left by one position.
=> arr[] = {2, 3, 4, 5, 6, 1}
Second Step:
=> Rotate again to left by one position
=> arr[] = {3, 4, 5, 6, 1, 2}
Rotation is done 2 times.
So the array becomes arr[] = {3, 4, 5, 6, 1, 2}
C++ C Java Python C# Javascript
// C++ Program to left rotate the array by d positions
// by rotating one element at a time
#include <bits/stdc++.h>
using namespace std;
// Function to left rotate array by d positions
void rotateArr(vector<int>& arr, int d) {
int n = arr.size();
// Repeat the rotation d times
for (int i = 0; i < d; i++) {
// Left rotate the array by one position
int first = arr[0];
for (int j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
We use cookies to ensure -you1]
arr[n have
= the best browsing experience on our website. By using our site, you
first;
acknowledge
} that you have read and understood our Cookie Policy & Privacy Policy
}
https://www.geeksforgeeks.org/dsa/array-rotation/ 2/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
int main() {
vector<int> arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Output
3 4 5 6 1 2
Time Complexity: O(n*d), the outer loop runs d times, and within each
iteration, the inner loop shifts all n elements of the array by one
position, resulting in a total of n*d operations.
Auxiliary Space: O(1)
[Better Approach] Using Temporary Array - O(n) Time and O(n)
Space
This problem can be solved using the below idea:
The idea is to use a temporary array of size n, where n is the
length of the original array. If we left rotate the array by d
positions, the last n - d elements will be at the front and the first
d elements will be at the end.
Copy the last (n - d) elements of original array into the first n -
d positions of temporary array.
Then copy the first d elements of the original array to the end
of temporary array.
Finally, copy all the elements of temporary array back into the
original array.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/dsa/array-rotation/ 3/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Working:
1/4
Below is the implementation of the algorithm:
C++ C Java Python C# Javascript
// C++ Program to left rotate the array by d positions
// using temporary array
#include <bits/stdc++.h>
using namespace std;
// Function to rotate vector
void rotateArr(vector<int>& arr, int d) {
int n = arr.size();
// Handle case when d > n
d %= n;
// Storing rotated version of array
vector<int> temp(n);
// Copy last n - d elements to the front of temp
for (int i = 0; i < n - d; i++)
temp[i] = arr[d + i];
// Copy
We use cookies theyoufirst
to ensure have thedbest
elements to the on
browsing experience back of temp
our website. By using our site, you
for (int that
acknowledge i =you
0;have
i read
< d;andi++)
understood our Cookie Policy & Privacy Policy
temp[n - d + i] = arr[i];
https://www.geeksforgeeks.org/dsa/array-rotation/ 4/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
// Copying the elements of temp in arr
// to get the final rotated vector
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
int main() {
vector<int> arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
// Print the rotated vector
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Output
3 4 5 6 1 2
Time Complexity: O(n), as we are visiting each element only twice.
Auxiliary Space: O(n), as we are using an additional temporary array.
[Expected Approach 1] Using Juggling Algorithm - O(n) Time
and O(1) Space
The idea is to use Juggling Algorithm which is based on rotating
elements in cycles. Each cycle is independent and represents a
group of elements that will shift among themselves during the
rotation. If the starting index of a cycle is i, then next elements of
the cycle can be found at indices (i + d) % n, (i + 2d) % n, (i + 3d)
% n ... and so on till we return to the original index i.
So for any index i, we know that after rotation, the element that
will occupy this position is arr[(i + d) % n]. Consequently, for
every index in the cycle, we will place the element that should be
in that position after the rotation is completed.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Please refer Juggling Algorithm for Array Rotation to know
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
more about the implementation.
https://www.geeksforgeeks.org/dsa/array-rotation/ 5/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Time Complexity: O(n)
Auxiliary Space: O(1)
[Expected Approach 2] Using Reversal Algorithm - O(n) Time
and O(1) Space
The idea is based on the observation that if we left rotate the
array by d positions, the last (n - d) elements will be at the front
and the first d elements will be at the end.
Reverse the subarray containing the first d elements of the
array.
Reverse the subarray containing the last (n - d) elements of
the array.
Finally, reverse all the elements of the array.
Working:
1/4
Below is the implementation of the algorithm:
C++ C Java Python C# JavaScript
// C++ Code to left rotate an array using Reversal Algorithm
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
#include that you have read and understood our Cookie Policy & Privacy Policy
<bits/stdc++.h>
https://www.geeksforgeeks.org/dsa/array-rotation/ 6/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
using namespace std;
// Function to rotate an array by d elements to the left
void rotateArr(vector<int>& arr, int d) {
int n = arr.size();
// Handle the case where d > size of array
d %= n;
// Reverse the first d elements
reverse(arr.begin(), arr.begin() + d);
// Reverse the remaining n-d elements
reverse(arr.begin() + d, arr.end());
// Reverse the entire array
reverse(arr.begin(), arr.end());
}
int main() {
vector<int> arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Output
3 4 5 6 1 2
Time complexity: O(n), as we are visiting each element exactly twice.
Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/dsa/array-rotation/ 7/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Rotate an Array Visit Course
Comment More info
Next Article
Campus Training Program Analysis of Algorithms
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Advertise with us
https://www.geeksforgeeks.org/dsa/array-rotation/ 8/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Company Explore
About Us Job-A-Thon
Legal Offline Classroom Program
Privacy Policy DSA in JAVA/C++
Careers Master System Design
In Media Master CP
Contact Us Videos
Corporate Solution
Campus Training Program
Tutorials DSA
Python DSA Tutorial
Java Problem Of The Day
C++ GfG 160
PHP DSA 360
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android
Data Science & ML Web Technologies
Data Science With Python HTML
Machine Learning CSS
ML Maths JavaScript
Data Visualisation TypeScript
Pandas ReactJS
NumPy NextJS
NLP NodeJs
Deep Learning Bootstrap
Tailwind CSS
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/dsa/array-rotation/ 9/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
Python Tutorial Computer Science
Python Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths
DevOps System Design
Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions
School Subjects Databases
Mathematics SQL
Physics MYSQL
Chemistry PostgreSQL
Biology PL/SQL
Social Science MongoDB
English Grammar
Preparation Corner More Tutorials
Company-Wise Recruitment Process Software Development
Aptitude Preparation Software Testing
Puzzles Product Management
Company-Wise Preparation Project Management
Linux
Excel
All Cheat Sheets
Courses Programming Languages
IBM Certification Courses C Programming with Data Structures
DSA and Placements C++ Programming Course
Web Development Java Programming Course
Data Science Python Full Course
Programming Languages
DevOps & Cloud
Clouds/Devops GATE 2026
DevOps Engineering GATE CS Rank Booster
We use cookies to ensure you have the best browsing experience on our website.
AWS Solutions Architect Certification
By using our site, you
GATE DA Rank Booster
acknowledge that you have read and
Salesforce Certified Administrator Course
understood our Cookie Policy & Privacy Policy
GATE CS & IT Course - 2026
GATE DA Course 2026
https://www.geeksforgeeks.org/dsa/array-rotation/ 10/11
04/08/2025, 07:24 Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks
GATE Rank Predictor
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/dsa/array-rotation/ 11/11