Print Array after it is right rotated K times where K can be large or negative
Last Updated :
21 Sep, 2022
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 = 2
Output: 7 9 1 3 5
Explanation:
Rotating array 1 time right: 9, 1, 3, 5, 7
Rotating array 2 time right: 7, 9, 1, 3, 5
Input: arr = {1, 2, 3, 4, 5}, K = -2
Output: 3 4 5 1 2
Explanation:
Rotating array -1 time right: 2, 3, 4, 5, 1
Rotating array -2 time right: 3, 4, 5, 1, 2
Naive Approach: The brute force approach to solve this problem is to use a temporary array to rotate the array K or -K times.
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: The given problem can be solved by breaking the problem into the following parts:
- Round up the value of K in range [0, N), using below steps:
- If K is negative, first change it into positive, find the modulo with N, and then again change it to negative
- If K is positive, just find the modulo with N
- Handle the case when K is negative. If K is negative, it means we need to rotate the array K times left, or -K times right.
- Next we can simply rotate the array K times 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(vector<int>& nums, int K)
{
int n = nums.size();
// Case when K > N or K < -N
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
// Case when K is negative
K = K < 0 ? (n - (K * -1)) : K;
// Reverse all the array elements
reverse(nums.begin(), nums.end());
// Reverse the first k elements
reverse(nums.begin(), nums.begin() + K);
// Reverse the elements from K
// till the end of the array
reverse(nums.begin() + K, nums.end());
}
// Driver code
int main()
{
// Initialize the array
vector<int> Array = { 1, 2, 3, 4, 5 };
// Find the size of the array
int N = Array.size();
// Initialize K
int K = -2;
// Call the function and
// print the answer
RightRotate(Array, K);
// Print the array after rotation
for (int i = 0; i < N; i++) {
cout << Array[i] << " ";
}
cout << endl;
return 0;
}
Java
// Java implementation for the above approach
import java.util.*;
class GFG{
// Initialize the array
static int[] Array = { 1, 2, 3, 4, 5 };
static void reverse( int start, int end) {
// Temporary variable to store character
int temp;
while (start <= end)
{
// Swapping the first and last character
temp = Array[start];
Array[start] = Array[end];
Array[end] = temp;
start++;
end--;
}
}
// Function to rotate the array
// to the right, K times
static void RightRotate( int K)
{
int n = Array.length;
// Case when K > N or K < -N
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
// Case when K is negative
K = K < 0 ? (n - (K * -1)) : K;
// Reverse all the array elements
reverse(0, n-1);
// Reverse the first k elements
reverse(0, n - K);
// Reverse the elements from K
// till the end of the array
reverse( K, n-1);
}
// Driver code
public static void main(String[] args)
{
// Find the size of the array
int N = Array.length;
// Initialize K
int K = -2;
// Call the function and
// print the answer
RightRotate(K);
// Print the array after rotation
for (int i = 0; i < N; i++) {
System.out.print(Array[i]+ " ");
}
System.out.println();
}
}
// This code is contributed by Rajput-Ji
Python3
# Python code for the above approach
# Function to rotate the array
# to the right, K times
def RightRotate(nums, K) :
n = len(nums)
# Case when K > N or K < -N
K = ((K * -1) % n) * -1 if K < 0 else K % n;
# Case when K is negative
K = (n - (K * -1)) if K < 0 else K;
# Reverse all the array elements
nums.reverse();
# Reverse the first k elements
p1 = nums[0:K]
p1.reverse();
# Reverse the elements from K
# till the end of the array
p2 = nums[K:]
p2.reverse();
arr = p1 + p2
return arr;
# Driver code
# Initialize the array
Array = [1, 2, 3, 4, 5];
# Find the size of the array
N = len(Array)
# Initialize K
K = -2;
# Call the function and
# print the answer
Array = RightRotate(Array, K);
# Print the array after rotation
for i in Array:
print(i, end=" ")
# This code is contributed by Saurabh jaiswal
C#
// C# implementation for the above approach
using System;
public class GFG {
// Initialize the array
static int[] Array = { 1, 2, 3, 4, 5 };
static void reverse(int start, int end)
{
// Temporary variable to store character
int temp;
while (start <= end) {
// Swapping the first and last character
temp = Array[start];
Array[start] = Array[end];
Array[end] = temp;
start++;
end--;
}
}
// Function to rotate the array
// to the right, K times
static void RightRotate(int K) {
int n = Array.Length;
// Case when K > N or K < -N
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
// Case when K is negative
K = K < 0 ? (n - (K * -1)) : K;
// Reverse all the array elements
reverse(0, n - 1);
// Reverse the first k elements
reverse(0, n - K);
// Reverse the elements from K
// till the end of the array
reverse(K, n - 1);
}
// Driver code
public static void Main(String[] args) {
// Find the size of the array
int N = Array.Length;
// Initialize K
int K = -2;
// Call the function and
// print the answer
RightRotate(K);
// Print the array after rotation
for (int i = 0; i < N; i++) {
Console.Write(Array[i] + " ");
}
Console.WriteLine();
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript code for the above approach
// Function to rotate the array
// to the right, K times
function RightRotate(nums, K)
{
let n = nums.length;
// Case when K > N or K < -N
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
// Case when K is negative
K = K < 0 ? (n - (K * -1)) : K;
// Reverse all the array elements
nums = nums.reverse();
// Reverse the first k elements
let p1 = nums.slice(0, K)
p1 = p1.reverse();
// Reverse the elements from K
// till the end of the array
let p2 = nums.slice(K)
p2 = p2.reverse();
let arr = p1.concat(p2);
return arr;
}
// Driver code
// Initialize the array
let Array = [1, 2, 3, 4, 5];
// Find the size of the array
let N = Array.length;
// Initialize K
let K = -2;
// Call the function and
// print the answer
Array = RightRotate(Array, K);
// Print the array after rotation
for (let i = 0; i < N; i++) {
document.write(Array[i] + " ");
}
document.write('<br>')
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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
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
Right rotate given Array K times using Pointers Given an array arr[] of size N and an integer K, the task is to right rotate the array K times. Examples: Input: arr[] = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5Explanation: After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5} Input: {1, 2, 3, 4, 5, 6}, K = 2Output: 5 6 1 2 3 4 Ap
6 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
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
11 min read
Javascript Program for 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 4A simple yet effective approach is to consider
2 min read