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 K kartik Follow Improve Article Tags : C++ rotation Practice Tags : CPP Similar Reads Javascript Program to Print array after it is right rotated K times 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: 2 min read Print array after it is right rotated K times Given an array arr[] and an integer k, rotate the array in place k times to the right (clockwise). In each rotation, the last element moves to the front, and all other elements shift one position to the right. Modify the array in place, do not return anything.Examples : Input: arr[] = [1, 2, 3, 4, 5 12 min read Print array after it is right rotated K times | Set 2 Given an array arr[] of size N and a value K, the task is to print the array rotated by K times to the right. Examples: Input: arr = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5 Input: arr = {1, 2, 3, 4, 5}, K = 4Output: 2 3 4 5 1 Algorithm: The given problem can be solved by reversing subarrays. Below s 13 min read Print Array after it is right rotated K times where K can be large or negative Given an array arr[] of size N and a value K (-10^5<K<10^5), the task is to print the array rotated by K times to the right. Examples: Input: arr = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5Explanation: Rotating array 1 time right: 9, 1, 3, 5, 7Rotating array 2 time right: 7, 9, 1, 3, 5 Input: ar 7 min read Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a 8 min read Like