Open In App

Rotate an Array - Clockwise or Right

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
17 Likes
Like
Report

Rotations in the array is defined as the process of rearranging the elements in an array by shifting each element to a new position. This is mostly done by rotating the elements of the array clockwise or counterclockwise.

Types of Rotations in Array

1. Right Rotation (or Clockwise)

Here, The array elements are shifted towards the right. 

left-array-rotation

2. Left Rotation (Or Counter Clockwise)

Here, The array elements are shifted towards the left.

right-arrow-Rotation

In this article, we will discuss about right rotation of the array. You can refer to Left rotate an array by d positions to know about the left rotation of the array.

How to implement rotations in an array?

There are several ways to implement array rotations. Some of the approaches are mentioned below. Here we are considering right rotation. The movements will be just the opposite for left rotation.

Input: arr[] = {1, 2, 3, 4, 5, 6}, d = 2
Output: {5, 6, 1, 2, 3, 4}
Explanation: After first right rotation, arr[] becomes {6, 1, 2, 3, 4, 5} and after the second rotation, arr[] becomes {5, 6, 1, 2, 3, 4}

Input: arr[] = {1, 2, 3}, d = 4
Output: {3, 1, 2}
Explanation: The array is rotated as follows:

  • After first left rotation, arr[] = {3, 1, 2}
  • After second left rotation, arr[] = {2, 3, 1}
  • After third left rotation, arr[] = {1, 2, 3}
  • After fourth left rotation, arr[] = {3, 1, 2}

1. Rotate one by one

At each iteration, shift the elements by one position to the right in a circular fashion (the last element becomes the first). Perform this operation d times to rotate the elements to the right by d positions.

Illustration:

Let us take arr[] = {1, 2, 3, 4, 5, 6}d = 2.

First Step:
        => Rotate to right by one position.
        => arr[] = {6, 1, 2, 3, 4, 5}
Second Step:
        => Rotate again to right by one position
        => arr[] = {5, 6, 1, 2, 3, 4}
Rotation is done 2 times.
So the array becomes arr[] = {5, 6, 1, 2, 3, 4}

C++
// C++ Program to right rotate the array by d positions
// by rotating one element at a time

#include <bits/stdc++.h>
using namespace std;

// Function to right 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++) {
      
        // Right rotate the array by one position
        int last = arr[n - 1];
        for (int j = n - 1; j > 0; j--) {
            arr[j] = arr[j - 1];
        }
        arr[0] = last;
    }
}

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 Java Python C# JavaScript

Output
5 6 1 2 3 4 

Time Complexity: O(n * d)
Auxiliary Space: O(1)

2. Using Temporary Array

The idea is to use a temporary array of size n, where n is the length of the original array. If we right rotate the array by d positions, the last d elements will be in the beginning and the first (n - d) elements will be at the end.

  • Copy the last d elements of the original array into the first d positions of the temporary array
  • Then copy the first n - 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.

Illustration for Right Rotation by 2 positions:


Below is the implementation of the above approach

C++
// C++ Program to right 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 d elements to the front of temp
    for (int i = 0; i < d; i++)
        temp[i] = arr[n - d + i];

    // Copy the first n - d elements to the back of temp
    for (int i = 0; i < n - d; i++)
        temp[i + d] = 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 Java Python C# JavaScript

Output
5 6 1 2 3 4 

Time complexity: O(n), where n is the size of input array arr[]. 
Auxiliary Space: O(n)

3.  Juggling Algorithm

The idea behind Juggling Algorithm is that instead of moving one by one, we can use the concept of 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 the next elements will be present at indices (i + d) % n, (i + 2d) % n, (i + 3d) % n ... and so on till we reach back to index i.

So for any index i, we know that element at index i will move to index (i + d) % n. Now, we can simply rotate all elements in the same cycle without interfering with any other cycle.

Working of the above algorithm:


Below is the implementation of the algorithm:

C++
// C++ Program to right rotate the array by d positions
// using Juggling Algorithm

#include <bits/stdc++.h>
using namespace std;

// Function to rotate vector
void rotateArr(vector<int> &arr, int d) {
    int n = arr.size();

    // Handle the case where d > size of array
    d %= n;

    // Calculate the number of cycles in the rotation
    int cycles = __gcd(n, d);

    // Process each cycle
    for (int i = 0; i < cycles; i++) {

        // Start index of current cycle
        int currIdx = i;
        int currEle = arr[currIdx];

        // Rotate elements till we reach the start of cycle
        do {
            int nextIdx = (currIdx + d) % n;
            int nextEle = arr[nextIdx];
          
            // Update the element at next index with the current element
            arr[nextIdx] = currEle;
          
            // Update the current element to next element
            currEle = nextEle;
          
            // Move to the next index
            currIdx = nextIdx;
        } while (currIdx != 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 Java Python C# JavaScript

Output
5 6 1 2 3 4 

4. The Reversal Algorithm

The idea is based on the observation that if we right rotate the array by d positions, the last d elements will be in the front and first (n - d) elements will be at the end.

  • First reverse all the elements of the array.
  • Then reverse first d elements.
  • Finally, reverse last (n - d) elements to get the final rotated array.

Illustration:


Below is the implementation of the above approach:

C++
// C++ Code to right rotate an array using Reversal Algorithm

#include <bits/stdc++.h>

using namespace std;

// Function to rotate an array by d elements to the right
void rotateArr(vector<int>& arr, int d) {
    int n = arr.size();
  
    // Handle the case where d > size of array
    d %= n;

  	// Reverse the entire array
    reverse(arr.begin(), arr.end());
  
    // Reverse the first d elements
    reverse(arr.begin(), arr.begin() + d);

    // Reverse the remaining n-d elements
    reverse(arr.begin() + d, 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 Java Python C# JavaScript

Output
5 6 1 2 3 4 

Time Complexity: O(n)
Auxiliary Space: O(1)


Left Rotation by d Places
Article Tags :

Similar Reads