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++
#include <bits/stdc++.h>
using namespace std;
void RightRotate(vector< int >& nums, int K)
{
int n = nums.size();
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
K = K < 0 ? (n - (K * -1)) : K;
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + K);
reverse(nums.begin() + K, nums.end());
}
int main()
{
vector< int > Array = { 1, 2, 3, 4, 5 };
int N = Array.size();
int K = -2;
RightRotate(Array, K);
for ( int i = 0; i < N; i++) {
cout << Array[i] << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int [] Array = { 1 , 2 , 3 , 4 , 5 };
static void reverse( int start, int end) {
int temp;
while (start <= end)
{
temp = Array[start];
Array[start] = Array[end];
Array[end] = temp;
start++;
end--;
}
}
static void RightRotate( int K)
{
int n = Array.length;
K = K < 0 ? ((K * - 1 ) % n) * - 1 : K % n;
K = K < 0 ? (n - (K * - 1 )) : K;
reverse( 0 , n- 1 );
reverse( 0 , n - K);
reverse( K, n- 1 );
}
public static void main(String[] args)
{
int N = Array.length;
int K = - 2 ;
RightRotate(K);
for ( int i = 0 ; i < N; i++) {
System.out.print(Array[i]+ " " );
}
System.out.println();
}
}
|
Python3
def RightRotate(nums, K) :
n = len (nums)
K = ((K * - 1 ) % n) * - 1 if K < 0 else K % n;
K = (n - (K * - 1 )) if K < 0 else K;
nums.reverse();
p1 = nums[ 0 :K]
p1.reverse();
p2 = nums[K:]
p2.reverse();
arr = p1 + p2
return arr;
Array = [ 1 , 2 , 3 , 4 , 5 ];
N = len (Array)
K = - 2 ;
Array = RightRotate(Array, K);
for i in Array:
print (i, end = " " )
|
C#
using System;
public class GFG {
static int [] Array = { 1, 2, 3, 4, 5 };
static void reverse( int start, int end)
{
int temp;
while (start <= end) {
temp = Array[start];
Array[start] = Array[end];
Array[end] = temp;
start++;
end--;
}
}
static void RightRotate( int K) {
int n = Array.Length;
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
K = K < 0 ? (n - (K * -1)) : K;
reverse(0, n - 1);
reverse(0, n - K);
reverse(K, n - 1);
}
public static void Main(String[] args) {
int N = Array.Length;
int K = -2;
RightRotate(K);
for ( int i = 0; i < N; i++) {
Console.Write(Array[i] + " " );
}
Console.WriteLine();
}
}
|
Javascript
<script>
function RightRotate(nums, K)
{
let n = nums.length;
K = K < 0 ? ((K * -1) % n) * -1 : K % n;
K = K < 0 ? (n - (K * -1)) : K;
nums = nums.reverse();
let p1 = nums.slice(0, K)
p1 = p1.reverse();
let p2 = nums.slice(K)
p2 = p2.reverse();
let arr = p1.concat(p2);
return arr;
}
let Array = [1, 2, 3, 4, 5];
let N = Array.length;
let K = -2;
Array = RightRotate(Array, K);
for (let i = 0; i < N; i++) {
document.write(Array[i] + " " );
}
document.write( '<br>' )
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)