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 = 2
Output: 7 9 1 3 5Input: arr = {1, 2, 3, 4, 5}, K = 4
Output: 2 3 4 5 1
Algorithm: The given problem can be solved by reversing subarrays. Below steps can be followed to solve the problem:
- Reverse all the array elements from 1 to N -1
- Reverse the array elements from 1 to K – 1
- Reverse the array elements from K to N -1
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to rotate the array // to the right, K times void RightRotate( int Array[], int N, int K) { // Reverse all the array elements reverse(Array, Array + N); // Reverse the first k elements reverse(Array, Array + K); // Reverse the elements from K // till the end of the array reverse(Array + K, Array + N); // Print the array after rotation for ( int i = 0; i < N; i++) { cout << Array[i] << " " ; } cout << endl; } // Driver code int main() { // Initialize the array int Array[] = { 1, 2, 3, 4, 5 }; // Find the size of the array int N = sizeof (Array) / sizeof (Array[0]); // Initialize K int K = 4; // Call the function and // print the answer RightRotate(Array, N, K); return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Function to rotate the array // to the right, K times static void RightRotate( int [] Array, int N, int K) { // Reverse all the array elements for ( int i = 0 ; i < N / 2 ; i++) { int temp = Array[i]; Array[i] = Array[N - i - 1 ]; Array[N - i - 1 ] = temp; } // Reverse the first k elements for ( int i = 0 ; i < K / 2 ; i++) { int temp = Array[i]; Array[i] = Array[K - i - 1 ]; Array[K - i - 1 ] = temp; } // Reverse the elements from K // till the end of the array for ( int i = 0 ; i < (K + N) / 2 ; i++) { int temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1 ) % N]; Array[(N - i + K - 1 ) % N] = temp; } // Print the array after rotation for ( int i = 0 ; i < N; i++) { System.out.print(Array[i] + " " ); } System.out.println(); } // Driver code public static void main(String[] args) { // Initialize the array int Array[] = { 1 , 2 , 3 , 4 , 5 }; // Find the size of the array int N = Array.length; // Initialize K int K = 4 ; // Call the function and // print the answer RightRotate(Array, N, K); } } // This code is contributed by maddler. |
Python3
# Python program for the above approach import math # Function to rotate the array # to the right, K times def RightRotate(Array, N, K): # Reverse all the array elements for i in range (math.ceil(N / 2 )): temp = Array[i] Array[i] = Array[N - i - 1 ] Array[N - i - 1 ] = temp # Reverse the first k elements for i in range (math.ceil(K / 2 )): temp = Array[i] Array[i] = Array[K - i - 1 ] Array[K - i - 1 ] = temp # Reverse the elements from K # till the end of the array for i in range (math.ceil((K + N) / 2 )): temp = Array[(i + K) % N] Array[(i + K) % N] = Array[(N - i + K - 1 ) % N] Array[(N - i + K - 1 ) % N] = temp # Print the array after rotation for i in range (N): print (Array[i], end = " " ) # Driver Code arr = [ 1 , 2 , 3 , 4 , 5 ] N = len (arr) K = 4 # Call the function and # print the answer RightRotate(arr, N, K) # This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approach using System; class GFG { // Function to rotate the array // to the right, K times static void RightRotate( int []Array, int N, int K) { // Reverse all the array elements for ( int i = 0; i < N / 2; i++) { int temp = Array[i]; Array[i] = Array[N - i - 1]; Array[N - i - 1] = temp; } // Reverse the first k elements for ( int i = 0; i < K / 2; i++) { int temp = Array[i]; Array[i] = Array[K - i - 1]; Array[K - i - 1] = temp; } // Reverse the elements from K // till the end of the array for ( int i = 0; i < (K + N) / 2; i++) { int temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1) % N]; Array[(N - i + K - 1) % N] = temp; } // Print the array after rotation for ( int i = 0; i < N; i++) { Console.Write(Array[i] + " " ); } } // Driver code public static void Main() { // Initialize the array int []Array = { 1, 2, 3, 4, 5 }; // Find the size of the array int N = Array.Length; // Initialize K int K = 4; // Call the function and // print the answer RightRotate(Array, N, K); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // Javascript program for the above approach // Function to rotate the array // to the right, K times function RightRotate(Array, N, K) { // Reverse all the array elements for (let i = 0; i < N / 2; i++) { let temp = Array[i]; Array[i] = Array[N - i - 1]; Array[N - i - 1] = temp; } // Reverse the first k elements for (let i = 0; i < K / 2; i++) { let temp = Array[i]; Array[i] = Array[K - i - 1]; Array[K - i - 1] = temp; } // Reverse the elements from K // till the end of the array for (let i = 0; i < (K + N) / 2; i++) { let temp = Array[(i + K) % N]; Array[(i + K) % N] = Array[(N - i + K - 1) % N]; Array[(N - i + K - 1) % N] = temp; } // Print the array after rotation for (let i = 0; i < N; i++) { document.write(Array[i] + " " ); } } // Driver Code let arr = [ 1, 2, 3, 4, 5 ]; let N = arr.length; let K =4; // Call the function and // print the answer RightRotate(arr, N, K); // This code is contributed by Samim Hossain Mondal. </script> |
Output
2 3 4 5 1
Time Complexity: O(N)
Auxiliary Space: O(1)