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}
[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++ 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];
}
arr[n - 1] = first;
}
}
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;
}
C
// C Program to left rotate the array by d positions
// by rotating one element at a time
#include <stdio.h>
// Function to left rotate array by d positions
// Repeat the rotation d times
void rotateArr(int arr[], int n, int d) {
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];
}
arr[n - 1] = first;
}
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
int d = 2;
int n = sizeof(arr) / sizeof(arr[0]);
rotateArr(arr, n, d);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to left rotate the array by d positions
// by rotating one element at a time
import java.util.Arrays;
class GfG {
// Function to left rotate array by d positions
static void rotateArr(int[] arr, int d) {
int n = arr.length;
// 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];
}
arr[n - 1] = first;
}
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
# Python Program to left rotate the array by d positions
# by rotating one element at a time
# Function to left rotate array by d positions
def rotateArr(arr, d):
n = len(arr)
# Repeat the rotation d times
for i in range(d):
# Left rotate the array by one position
first = arr[0]
for j in range(n - 1):
arr[j] = arr[j + 1]
arr[n - 1] = first
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6]
d = 2
rotateArr(arr, d)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to left rotate the array by d positions
// by rotating one element at a time
using System;
class GfG {
// Function to left rotate array by d positions
static void rotateArr(int[] arr, int d) {
int n = arr.Length;
// 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];
}
arr[n - 1] = first;
}
}
static void Main() {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
Javascript
// JavaScript Program to left rotate the array by d positions
// by rotating one element at a time
// Function to left rotate array by d positions
function rotateArr(arr, d) {
let n = arr.length;
// Repeat the rotation d times
for (let i = 0; i < d; i++) {
// Left rotate the array by one position
let first = arr[0];
for (let j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
arr[n - 1] = first;
}
}
let arr = [1, 2, 3, 4, 5, 6];
let d = 2;
rotateArr(arr, d);
console.log(arr.join(" "));
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.
Working:
Below is the implementation of the algorithm:
C++
// 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 the first d elements to the back of temp
for (int i = 0; i < d; i++)
temp[n - d + i] = arr[i];
// 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;
}
C
// C Program to left rotate the array by d positions
// using temporary array
#include <stdio.h>
#include <stdlib.h>
// Function to rotate array
void rotateArr(int* arr, int d, int n) {
// Handle case when d > n
d %= n;
// Storing rotated version of array
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 the first d elements to the back of temp
for (int i = 0; i < d; i++)
temp[n - d + i] = arr[i];
// Copying the elements of temp in arr
// to get the final rotated array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int d = 2;
rotateArr(arr, d, n);
// Print the rotated array
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to left rotate the array by d positions
// using temporary array
import java.util.Arrays;
class GfG {
// Function to rotate array
static void rotateArr(int[] arr, int d) {
int n = arr.length;
// Handle case when d > n
d %= n;
// Storing rotated version of array
int[] temp = new int[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 the first d elements to the back of temp
for (int i = 0; i < d; i++)
temp[n - d + i] = arr[i];
// Copying the elements of temp in arr
// to get the final rotated array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
// Print the rotated array
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
# Python Program to left rotate the array by d positions
# using temporary array
# Function to rotate array
def rotateArr(arr, d):
n = len(arr)
# Handle case when d > n
d %= n
# Storing rotated version of array
temp = [0] * n
# Copy last n - d elements to the front of temp
for i in range(n - d):
temp[i] = arr[d + i]
# Copy the first d elements to the back of temp
for i in range(d):
temp[n - d + i] = arr[i]
# Copying the elements of temp in arr
# to get the final rotated array
for i in range(n):
arr[i] = temp[i]
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6]
d = 2
rotateArr(arr, d)
# Print the rotated array
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to left rotate the array by d positions
// using temporary array
using System;
class GfG {
// Function to rotate array
static void rotateArr(int[] arr, int d) {
int n = arr.Length;
// Handle case when d > n
d %= n;
// Storing rotated version of array
int[] temp = new int[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 the first d elements to the back of temp
for (int i = 0; i < d; i++)
temp[n - d + i] = arr[i];
// Copying the elements of temp in arr
// to get the final rotated array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
// Print the rotated array
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
Javascript
// JavaScript Program to left rotate the array by d positions
// using temporary array
// Function to rotate array
function rotateArr(arr, d) {
let n = arr.length;
// Handle case when d > n
d %= n;
// Storing rotated version of array
let temp = new Array(n);
// Copy last n - d elements to the front of temp
for (let i = 0; i < n - d; i++)
temp[i] = arr[d + i];
// Copy the first d elements to the back of temp
for (let i = 0; i < d; i++)
temp[n - d + i] = arr[i];
// Copying the elements of temp in arr
// to get the final rotated array
for (let i = 0; i < n; i++)
arr[i] = temp[i];
}
const arr = [1, 2, 3, 4, 5, 6];
const d = 2;
rotateArr(arr, d);
// Print the rotated array
console.log(arr.join(" "));
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.
Please refer Juggling Algorithm for Array Rotation to know more about the implementation.
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:
Below is the implementation of the algorithm:
C++
// C++ Code to left rotate an array using Reversal Algorithm
#include <bits/stdc++.h>
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;
}
C
// C Code to left rotate an array using Reversal Algorithm
#include <stdio.h>
// Function to reverse a portion of the array
void reverse(int* arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
// Function to rotate an array by d elements to the left
void rotateArr(int* arr, int n, int d) {
// Handle the case where d > size of array
d %= n;
// Reverse the first d elements
reverse(arr, 0, d - 1);
// Reverse the remaining n-d elements
reverse(arr, d, n - 1);
// Reverse the entire array
reverse(arr, 0, n - 1);
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int d = 2;
rotateArr(arr, n, d);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Code to left rotate an array using Reversal Algorithm
import java.util.Arrays;
class GfG {
// Function to rotate an array by d elements to the left
static void rotateArr(int[] arr, int d) {
int n = arr.length;
// Handle the case where d > size of array
d %= n;
// Reverse the first d elements
reverse(arr, 0, d - 1);
// Reverse the remaining n-d elements
reverse(arr, d, n - 1);
// Reverse the entire array
reverse(arr, 0, n - 1);
}
// Function to reverse a portion of the array
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--;
}
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
# Python Code to left rotate an array using Reversal Algorithm
# Function to rotate an array by d elements to the left
def rotateArr(arr, d):
n = len(arr)
# Handle the case where d > size of array
d %= n
# Reverse the first d elements
reverse(arr, 0, d - 1)
# Reverse the remaining n-d elements
reverse(arr, d, n - 1)
# Reverse the entire array
reverse(arr, 0, n - 1)
# Function to reverse a portion of the array
def reverse(arr, start, end):
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6]
d = 2
rotateArr(arr, d)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Code to left rotate an array using Reversal Algorithm
using System;
class GfG {
// Function to rotate an array by d elements to the left
static void rotateArr(int[] arr, int d) {
int n = arr.Length;
// Handle the case where d > size of array
d %= n;
// Reverse the first d elements
reverse(arr, 0, d - 1);
// Reverse the remaining n-d elements
reverse(arr, d, n - 1);
// Reverse the entire array
reverse(arr, 0, n - 1);
}
// Function to reverse a portion of the array
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 Main() {
int[] arr = { 1, 2, 3, 4, 5, 6 };
int d = 2;
rotateArr(arr, d);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Code to left rotate an array using Reversal Algorithm
// Function to rotate an array by d elements to the left
function rotateArr(arr, d) {
let n = arr.length;
// Handle the case where d > size of array
d %= n;
// Reverse the first d elements
reverse(arr, 0, d - 1);
// Reverse the remaining n-d elements
reverse(arr, d, n - 1);
// Reverse the entire array
reverse(arr, 0, n - 1);
}
// Function to reverse a portion of the array
function reverse(arr, start, end) {
while (start < end) {
let temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
const arr = [1, 2, 3, 4, 5, 6];
const d = 2;
rotateArr(arr, d);
console.log(arr.join(" "));
Time complexity: O(n), as we are visiting each element exactly twice.
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem