C++ Program to Print array after it is right rotated K times Last Updated : 25 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples : Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: Array[] = {1, 2, 3, 4, 5}, K = 4. Output: 2 3 4 5 1 Approach: We will first take mod of K by N (K = K % N) because after every N rotations array will become the same as the initial array. Now, we will iterate the array from i = 0 to i = N-1 and check, If i < K, Print rightmost Kth element (a[N + i -K]). Otherwise, Print array after 'K' elements (a[i - K]). Below is the implementation of the above approach. C++ // C++ implementation of right rotation // of an array K number of times #include<bits/stdc++.h> using namespace std; // Function to rightRotate array void RightRotate(int a[], int n, int k) { // If rotation is greater // than size of array k = k % n; for(int i = 0; i < n; i++) { if(i < k) { // Printing rightmost // kth elements cout << a[n + i - k] << " "; } else { // Prints array after // 'k' elements cout << (a[i - k]) << " "; } } cout << " "; } // Driver code int main() { int Array[] = { 1, 2, 3, 4, 5 }; int N = sizeof(Array) / sizeof(Array[0]); int K = 2; RightRotate(Array, N, K); } // This code is contributed by Surendra_Gangwar Output: 4 5 1 2 3 Time complexity : O(n) Auxiliary Space : O(1) Please refer complete article on Print array after it is right rotated K times for more details! Comment More infoAdvertise with us Next Article C++ Program to Print array after it is right rotated K times kartik Follow Improve Article Tags : C++ Programs C++ DSA Arrays rotation +1 More Practice Tags : CPPArrays Similar Reads C++ Program to Rotate the matrix right by K times Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider 2 min read C++ Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat 3 min read C++ Program to Find the Mth element of the Array after K left rotations Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation: The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ] 3 min read C++ Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 min read 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}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 eleme 3 min read C++ Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required 2 min read C++ Program to Split the array and add the first part to the end | Set 2 Given an array and split it from a specified position, and move the first part of array add to the end.  Examples:  Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2 Output : arr[] = {5, 6, 52, 36, 12, 10} Explanation : Split from index 2 and first part {12, 10} add to the end . Input : arr[] = {3, 1, 2 min read C++ Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6 2 min read 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 for Left Rotation and Right Rotation of a String Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : 5 min read Like