-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularRotation.cpp
58 lines (53 loc) · 1.21 KB
/
CircularRotation.cpp
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
vector<long int> arr ;
long int size,r,q;
cin>>size;
cin>>r;
cin>>q;
for(long int i=0;i<size;i++)
{
long int x;
cin>>x;
arr.push_back(x);
}
//O(n*r)
/*for(long int j=0;j<r;j++){
long int l = arr[size-1];
for(long int i=size-1;i>0;i--)
{
arr[i]=arr[i-1];
}
arr[0]=l;
}*/
// 2nd Method O(n)
int rot=r%size;
for(long int i=0;i<q;i++)
{
long int a;
cin>>a;
if(a-rot>=0)
cout<<arr[a-rot]<<endl;
else
cout<<arr[a-rot+size]<<endl;
}
/*It's just because in the '(rot-1)th' elements of the array you will have negative indexes,
causing errors or, even, segmentation error (as in test case 4). So, doing this, you add N to
negative indexes to retrive the correct values from the former array, without rotating it at all
or
do
cin>> arr[(i+r)%n];
*/
/*for(long int i=0;i<q;i++)
{
long int a;
cin>>a;
cout<<arr[a]<<endl;
}*/
return 0;
}