Sunday, October 12, 2025
HomeData Modelling & AIC++ Program to cyclically rotate an array by one

C++ Program to cyclically rotate an array by one

Given an array, cyclically rotate the array clockwise by one. 

Examples:  

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

Following are steps. 
1) Store last element in a variable say x. 
2) Shift all elements one position ahead. 
3) Replace first element of array with x.
 

C++




// C++ code for program 
// to cyclically rotate
// an array by one
# include <iostream>
using namespace std;
  
void rotate(int arr[], int n)
{
    int x = arr[n - 1], i;
    for (i = n - 1; i > 0; i--)
    arr[i] = arr[i - 1]; 
    arr[0] = x;
}
  
// Driver code
int main() 
{
    int arr[] = {1, 2, 3, 4, 5}, i;
    int n = sizeof(arr) / 
            sizeof(arr[0]);
  
    cout << "Given array is 
";
    for (i = 0; i < n; i++)
        cout << arr[i] << ' ';
  
    rotate(arr, n);
  
    cout << "
Rotated array is
";
    for (i = 0; i < n; i++)
        cout << arr[i] << ' ';
  
    return 0;
}
  
// This code is contributed by jit_t


Output

Given array is 
1 2 3 4 5 

Rotated array is
5 1 2 3 4 

Time Complexity: O(n) As we need to iterate through all the elements 
Auxiliary Space: O(1)
The above question can also be solved by using reversal algorithm.

Another approach:

We can use two pointers, say i and j which point to first and last element of array respectively. As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, so start swapping arr[i] and arr[j] and keep j fixed and i moving towards j.  Repeat till i is not equal to j.

C++




#include <iostream>
using namespace std;
  
void rotate(int arr[], int n)
{
    int i = 0, j = n-1; // i and j pointing to first and last element respectively
    while(i != j){
    swap(arr[i], arr[j]);
    i++;
    }
}
  
// Driver code
int main() 
{
    int arr[] = {1, 2, 3, 4, 5}, i;
    int n = sizeof(arr) / 
            sizeof(arr[0]);
  
    cout << "Given array is \n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    rotate(arr, n);
  
    cout << "\nRotated array is\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}


Output

Given array is 
1 2 3 4 5 
Rotated array is
5 1 2 3 4 

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

Please refer complete article on Program to cyclically rotate an array by one for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7105 POSTS0 COMMENTS
Thapelo Manthata
6796 POSTS0 COMMENTS
Umr Jansen
6795 POSTS0 COMMENTS