-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotateArray.java
41 lines (37 loc) · 1013 Bytes
/
rotateArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void rotate(int[] arr, int d) {
int[] temp= new int[arr.length];
for(int i=0;i<arr.length;i++)
temp[i]=arr[i];
for(int i=0;i<arr.length;i++)
{
if(i-d>=0)
arr[i-d]=temp[i];
else
arr[arr.length+(i-d)]=temp[i];
}
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
while(testCases-->0){
//taking size of array and creating array
int size = sc.nextInt();
int rotateIndex = sc.nextInt();
int arr[] = new int[size];
//taking input of arrays
for(int i=0;i<size;i++){
arr[i] = sc.nextInt();
}
rotate(arr,rotateIndex);
for(int i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}