Print array after it is right rotated K times | Set 2 - GeeksforGeeks

26 Nov, 2021
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 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++
Java
Python3
C#
Javascript
// C++ implementation for the above approach #include <bits/stdc++.h>using namespace std; // Function to rotate the array// to the right, K timesvoid 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 codeint 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;}
/*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.
# Python program for the above approachimport math # Function to rotate the array# to the right, K timesdef 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 Codearr = [1, 2, 3, 4, 5]N = len(arr)K = 4 # Call the function and# print the answerRightRotate(arr, N, K) # This code is contributed by Saurabh Jaiswal
// C# program for the above approachusing 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.
<script>// Javascript program for the above approach // Function to rotate the array// to the right, K timesfunction 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 answerRightRotate(arr, N, K); // This code is contributed by Samim Hossain Mondal.</script>
2 3 4 5 1 
Time Complexity: O(N)  Auxiliary Space: O(1)
lokeshpotta20
maddler
samim2000
_saurabh_jaiswal
Reverse
rotation
subarray
Arrays
Arrays
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Remove duplicates from sorted array
Move all negative numbers to beginning and positive to end with constant extra space
Merge Overlapping Intervals