-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularArrayRotation.java
More file actions
70 lines (54 loc) · 1.57 KB
/
CircularArrayRotation.java
File metadata and controls
70 lines (54 loc) · 1.57 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class CircularArrayRotation {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k= in.nextInt();
int q= in.nextInt();
int[] arr=new int[n];
int [] indexArr=new int[q];
for(int count=0;count<n;count++){
arr[count]=in.nextInt();
}
for(int count=0;count<q;count++){
indexArr[count]=in.nextInt();
}
arr=shiftKTimes(arr,k);
for(int count=0;count<q;count++){
System.out.println(arr[indexArr[count]]);
}
}
static int [] shiftKTimes(int arr[],int k){
int temp=0;
// int loc=0;
// for(int count=0;count<arr.length;count++){
// loc=count+k;
// if(loc>arr.length-1)
// loc=loc-(arr.length-1);
// temp=arr[count];
// arr[count]=arr[loc];
// arr[loc]=temp;
// }
for(int count=0;count<k;count++){
arr=shift(arr);
}
return arr;
}
static int [] shift(int arr[]){
int lastElement=arr[(arr.length-1)];
for (int count=(arr.length-1);count>0;count--){
arr[count]=arr[count-1];
}
if(arr.length>=1)
arr[0]=lastElement;
return arr;
}
// void swap(int a, int b){
// a
// }
}