C++ Program to cyclically rotate an array by one Last Updated : 19 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. 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 OutputGiven 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; } OutputGiven 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! Comment More infoAdvertise with us Next Article C++ Program to cyclically rotate an array by one K kartik Follow Improve Article Tags : C++ Programs C++ Computer Science Fundamentals DSA Arrays rotation +2 More Practice Tags : CPPArrays Similar Reads C++ Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar 5 min read C++ Program to Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes". Examples: Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation: After 2 min read C++ Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put ba 3 min read C++ Program to Rotate Doubly linked list by N nodes Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d As 4 min read C++ Program to Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints ro 6 min read Like