-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. Rotate Array
68 lines (58 loc) · 1.41 KB
/
4. Rotate Array
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
//{ Driver Code Starts
// Initial function template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to rotate an array by d elements in counter-clockwise direction.
void rotateArr(vector<int>& arr, int d) {
// code here
int n = arr.size();
if(n <= 1)
{
return;
}
d = d % n;
reverse(arr.begin(), arr.begin() + d);
reverse(arr.begin() + d, arr.end());
reverse(arr.begin(), arr.end());
}
};
//{ Driver Code Starts.
int main() {
int test_case;
cin >> test_case;
cin.ignore();
while (test_case--) {
int d;
vector<int> arr, brr, crr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
getline(cin, input);
ss.clear();
ss.str(input);
while (ss >> number) {
crr.push_back(number);
}
d = crr[0];
int n = arr.size();
Solution ob;
// calling rotateArr() function
ob.rotateArr(arr, d);
// printing the elements of the array
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends