Open In App

Rotate Array By One

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

Given an array, the task is to cyclically right-rotate the array by one. 

Examples:  

Input: arr[] = [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]

Input: arr[] = [2, 3, 4, 5, 1]
Output: [1, 2, 3, 4, 5]

Shifting Each Element - O(n) Time and O(1) Space

The basic idea is to store the last element in a temp variable and shift every other element one position ahead. After shifting, update the first element with value stored in temp.

C++14
#include <iostream>
#include<vector>
using namespace std;

void rotate(vector<int> &arr) {
  int n = arr.size();
  
    // store the last element in a variable
    int lastElement = arr[n-1];

    // assign every value by its predecessor
    for (int i = n - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }

    // first element will be assigned by last element
    arr[0] = lastElement;
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5 };

    rotate(arr);
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << ' ';

    return 0;
}
C Java Python C# JavaScript

Output
5 1 2 3 4 

Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.

Using Two Pointers - O(n) Time and O(1) Space

We can use two pointers, As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, we can do this by swapping every element with last element till we get to the last point.

C++14
#include <iostream>
#include<vector>
using namespace std;

void rotate(vector<int> &arr) {
	int n=arr.size();
  
    // i and j pointing to first and last
    // element respectively
    int i = 0, j = n - 1;
    while (i != j) {
        swap(arr[i], arr[j]);
        i++;
    }
}

int main() {
 	vector<int> arr = {1, 2, 3, 4, 5 };
 	int i;
    rotate(arr);
    for (i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";

    return 0;
}
C Java Python C# JavaScript

Output
5 1 2 3 4 

Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.

Reversing the Array - O(n) Time and O(1) Space

We can use Reversal Algorithm also, reverse first n-1 elements and then whole array which will result into one right rotation.

C++14
#include <iostream>
#include<vector>
using namespace std;

void rotate(vector<int> &arr) {
  int n = arr.size();
  
    // Reverse the first n-1 terms
    int i, j;
    for (i = 0, j = n - 2; i < j; i++, j--) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
  
    // Reverse the entire array
    for (i = 0, j = n - 1; i < j; i++, j--) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5 };
    int i, j;
    rotate(arr);
    for (i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";

    return 0;
}
C Java Python C# JavaScript

Output
5 1 2 3 4 

Time Complexity: O(n), as we are reversing the array. Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.


Program to cyclically rotate an array by one
Practice Tags :

Similar Reads