C++ Program to cyclically rotate an array by one

09 Jul, 2022
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 codeint 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
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 swaping 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 codeint 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;}
Given array is 
1 2 3 4 5 
Rotated array is
5 1 2 3 4 
Time Complexity: O(n)Auxiliary Space: O(1)
Program to cyclically rotate an array by one | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersProgram to cyclically rotate an array by one | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:45•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ZNqWgwwpgLU" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please refer complete article on Program to cyclically rotate an array by one for more details!
sachinvinod1904
rotation
Arrays
C++
C++ Programs
School Programming
Arrays
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
Chocolate Distribution Problem
What is Data Structure: Types, Classifications and Applications
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++