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 << "\n"; } // 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 |
Java
// Java Implementation of Right Rotation // of an Array K number of times import java.util.*; import java.lang.*; import java.io.*; class Array_Rotation { // Function to rightRotate array static 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 System.out.print(a[n + i - k] + " "); } else { // Prints array after // 'k' elements System.out.print(a[i - k] + " "); } } System.out.println(); } // Driver program public static void main(String args[]) { int Array[] = {1, 2, 3, 4, 5}; int N = Array.length; int K = 2; RightRotate(Array, N, K); } } // This code is contributed by M Vamshi Krishna |
Python3
# Python3 implementation of right rotation # of an array K number of times # Function to rightRotate array def RightRotate(a, n, k): # If rotation is greater # than size of array k = k % n; for i in range(0, n): if(i < k): # Printing rightmost # kth elements print(a[n + i - k], end = " "); else: # Prints array after # 'k' elements print(a[i - k], end = " "); print("\n"); # Driver code Array = [ 1, 2, 3, 4, 5 ]; N = len(Array); K = 2; RightRotate(Array, N, K); # This code is contributed by Code_Mech |
C#
// C# implementation of right rotation // of an array K number of times using System; class GFG{ // Function to rightRotate array static 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 Console.Write(a[n + i - k] + " "); } else { // Prints array after // 'k' elements Console.Write(a[i - k] + " "); } } Console.WriteLine(); } // Driver code public static void Main(String []args) { int []Array = { 1, 2, 3, 4, 5 }; int N = Array.Length; int K = 2; RightRotate(Array, N, K); } } // This code is contributed by Rohit_ranjan |
4 5 1 2 3
Time complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Search an element in a sorted and rotated array
- Find the minimum element in a sorted and rotated array
- Given a sorted and rotated array, find if there is a pair with a given sum
- Find the Rotation Count in Rotated Sorted array
- Count elements less than or equal to a given value in a sorted rotated array
- Sort a Rotated Sorted Array
- Search an element in a sorted and rotated array with duplicates
- Maximum element in a sorted and rotated array
- Check if an array is sorted and rotated using Binary Search
- Check if an array is sorted and rotated
- Sum of array elements after every element x is XORed itself x times
- Check if the last element of array is even or odd after performing a operation p times
- Count all elements in the array which appears at least K times after their first occurrence
- Find the number of different numbers in the array after applying the given operation q times
- Check if original Array is retained after performing XOR with M exactly K times
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Rotate the matrix right by K times
- Lexicographically smallest rotated sequence | Set 2
- Count rotations in sorted and rotated linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

