-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-array-in-groups.cpp
59 lines (50 loc) · 1.17 KB
/
reverse-array-in-groups.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
59
//{ Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
//Function to reverse every sub-array group of size k.
void reverseInGroups(vector<long long>& arr, int n, int k){
// total groups
int no_of_groups = n/k;
// remaining elements
int remaining = n%k;
int i = 0;
while (no_of_groups)
{
reverse(arr.begin() + i, arr.begin() + i + k);
i += k;
no_of_groups--;
}
reverse(arr.end() - remaining, arr.end());
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<long long> arr;
int k;
cin >> k;
for(long long i = 0; i<n; i++)
{
long long x;
cin >> x;
arr.push_back(x);
}
Solution ob;
ob.reverseInGroups(arr, n, k);
for(long long i = 0; i<n; i++){
cout << arr[i] << " ";
}
cout << endl;
}
return 0;
}
// } Driver Code Ends