-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicRotation.cpp
More file actions
51 lines (47 loc) · 1.12 KB
/
CyclicRotation.cpp
File metadata and controls
51 lines (47 loc) · 1.12 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
#include "all.h"
vector<int> solution(vector<int> &A, int K) {
// write your code in C++14 (g++ 6.2.0)
vector<int> result;
if (A.size() == 0) {
return result;
}else if (A.size() == 1 || K%A.size() == 0) {
return A;
}else {
if (K >= A.size()) {
K = K%A.size();
}
for (auto it = A.end()-K; it != A.end(); ++it) {
result.push_back(*it);
// cout << *it << endl;
}
for (auto it = A.begin(); it != A.end()-K; ++it) {
result.push_back(*it);
}
}
return result;
}
int main(void) {
vector<int> answer;
vector<int> A = {3, 8, 9, 7, 6};
answer = solution(A, 3);
cout << "Expected [ 9 7 6 3 8 ]. Got [ ";
for (int x:answer) {
cout << x << " ";
}
cout << "]" << endl;
A = {0, 0, 0};
answer = solution(A, 1);
cout << "Expected [ 0 0 0 ]. Got [ ";
for (int x:answer) {
cout << x << " ";
}
cout << "]" << endl;
A = {1, 2, 3, 4};
answer = solution(A, 4);
cout << "Expected [ 1 2 3 4 ]. Got [ ";
for (int x:answer) {
cout << x << " ";
}
cout << "]" << endl;
return 0;
}