forked from peehu-gandhi/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse array in groups
46 lines (39 loc) · 932 Bytes
/
Reverse array in groups
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
#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){
int i=0;
for(i=0;i<n-k;i+=k)
reverse(arr.begin()+i,arr.begin()+k+i);
reverse(arr.begin()+i,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;
}
}
// } Driver Code Ends