Rotate Array | Practice | GeeksforGeeks

Given an unsorted array arr[] of size N, rotate it by D elements (Anti - clockwise). 
Input:
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.
Output: 
For each testcase, in a new line, output the rotated array.
Constraints:
1 <= T <= 200
1 <= N <= 107
1 <= D <= N
0 <= arr[i] <= 105
Example:
Input:
2
5 2
1 2 3 4 5 
10 3
2 4 6 8 10 12 14 16 18 20
Output:
3 4 5 1 2
8 10 12 14 16 18 20 2 4 6
Explanation :
Testcase 1: 1 2 3 4 5  when rotated by 2 elements, it becomes 3 4 5 1 2.
 
0
manishkumar8969012 weeks ago
#include <iostream>#include<bits/stdc++.h>using namespace std;
void rotation(int nums[],int D,int N){   int arr[N];   for(int i=0;i<N;i++)   {       arr[i]=nums[i];   }    for(int i=0;i<N;i++)       {           nums[(i-D+N)%N]=arr[i];       }       for(int x=0;x<N;x++)       {           cout<<nums[x]<<" ";       }}int main() {int T;cin>>T;for(int i=0;i<T;i++){    int N,D;cin>>N>>D;int nums[N];for(int i=0;i<N;i++){    cin>>nums[i];}rotation(nums,D,N);cout<<"\n";   }return 0;}
0
geeky_kuldeep1 month ago
JS Solution

function rotate(arr, n, d) {
   let res = [];
   for (let i = d; i < n; i++) {
       res.push(arr[i]);
   }
   for (let i = 0; i < d; i++) {
       res.push(arr[i]);
   }
   return res;
}
0
ashutosh0366tripathi1 month ago
a=int(input())for i in range(a):   n,s1=map(int,input().split())   arr=list(map(int,input().split()))            for i in range(s1):       temp=arr[0]       for j in range(len(arr)-1):           arr[j]=arr[j+1]                  arr[len(arr)-1]=temp          print(" ".join(list(map(str,arr))))
0
shantanupatil9582 months ago
class GFG {public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt();   for(int i = 0; i < t; i++){        int n = sc.nextInt();  int d = sc.nextInt();   int[] arr = new int[n];      for(int index = 0; index < n; index++){      arr[index] = sc.nextInt();        }        for(int index2 = d; index2 < arr.length; index2++ ){       System.out.print(arr[index2] + " ");          }       for(int index3 = 0; index3 < d; index3++){           System.out.print(arr[index3]+ " ");        }      System.out.println(" ");  }  }}
-1
ssatyamlal2 months ago
//JAVA CODE
 
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args) {     Scanner user = new Scanner(System.in);     int t = user.nextInt();          while(t > 0){         int n = user.nextInt();         int rotation = user.nextInt();         int[] arr = new int[n];                  for(int i = 0; i < n; i++){             arr[i] = user.nextInt();         }                  for(int i = rotation; i < n; i++){             System.out.print(arr[i] + " ");         }                  for(int i = 0; i < rotation; i++){             System.out.print(arr[i] + " ");         }         System.out.println();         t--;     } }}
0
ramdurgasais2 months ago
 In Python

for _ in range(int(input())):
    
    n, d = list(map(int,input().split(" ")))
    
    elements = list(map(int,input().split()))
    
    print(" ".join(list(map(str,elements[d:] + elements[:d]))))
0
ramdurgasais2 months ago
#In Python
for _ in range(int(input())):      n, d = list(map(int,input().split(" ")))      elements = list(map(int,input().split()))      print(" ".join(list(map(str,elements[d:] + elements[:d]))))
0
ayazmrz982 months ago
 public static void main (String[] args) {
 Scanner scanner = new Scanner(System.in);
 int testcases= scanner.nextInt();
 while(testcases-- >0)
 {
     int size = scanner.nextInt();
     int rotatingindex = scanner.nextInt();
     int[] array = new int[size];
     for(int i=0;i<size;i++)
     {
         array[i]=scanner.nextInt();
     }
     
     for(int i=rotatingindex;i<size;i++)
     {
         System.out.print(array[i]+" ");
     }
     
      for(int i=0;i<rotatingindex;i++)
     {
         System.out.print(array[i]+" ");
     }
     
     System.out.println();
    
    
 }
}
0
muskang87012 months ago
Java Code-
 
class GFG {
public static void main (String[] args) {
 //code
 Scanner sc = new Scanner(System.in);
 int t = sc.nextInt();
 while(t-- > 0) {
     int n = sc.nextInt();
     int d = sc.nextInt();
     int a[] = new int[n];
     ArrayList<Integer> b = new ArrayList<>();
     for(int i = 0; i < n; i++) {
         a[i] = sc.nextInt();
     }
     for(int i = 0; i < n; i++) {
         if((i+d) == n)
         break;
         b.add(a[i+d]);
     }
     for(int i = 0; i < n-(n-d); i++) {
         b.add(a[i]);
     }
     for(int i = 0; i < b.size(); i++) {
         System.out.print(b.get(i) + " ");
     }
     System.out.println();
 }
}
}
+1
codewithaddy2 months ago
C++ Solution (Basic)
 
#include <iostream>using namespace std;
void rotate(int a[], int n, int r){   int b[r];   for(int i=0; i<r; i++){       b[i]=a[i];   }   for(int i=0; i<n-r; i++){       a[i]=a[i+r];   }    for(int i=0; i<n-r; i++){       cout<<a[i]<<" ";   }    for(int i=0; i<r; i++){       cout<<b[i]<<" ";   }        }
int main() {int t;cin>>t;while(t--){    int n,r;    cin>>n>>r;    int a[n];    for(int i=0; i<n; i++){        cin>>a[i];    }        rotate(a,n,r);    cout<<endl;}return 0;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.