Skip to content

Commit 0a697e5

Browse files
committed
Added solution - LeetHub
1 parent 1e66f7d commit 0a697e5

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//{ Driver Code Starts
2+
//Initial template for C++
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
8+
// } Driver Code Ends
9+
//User function template for C++
10+
11+
class Solution{
12+
public:
13+
//Function to reverse every sub-array group of size k.
14+
void reverseInGroups(vector<long long>& arr, int n, int k){
15+
// total groups
16+
int no_of_groups = n/k;
17+
// remaining elements
18+
int remaining = n%k;
19+
int i = 0;
20+
while (no_of_groups)
21+
{
22+
reverse(arr.begin() + i, arr.begin() + i + k);
23+
i += k;
24+
no_of_groups--;
25+
}
26+
reverse(arr.end() - remaining, arr.end());
27+
}
28+
};
29+
30+
//{ Driver Code Starts.
31+
int main() {
32+
int t;
33+
cin >> t;
34+
while(t--){
35+
int n;
36+
cin >> n;
37+
vector<long long> arr;
38+
int k;
39+
cin >> k;
40+
41+
for(long long i = 0; i<n; i++)
42+
{
43+
long long x;
44+
cin >> x;
45+
arr.push_back(x);
46+
}
47+
Solution ob;
48+
ob.reverseInGroups(arr, n, k);
49+
50+
for(long long i = 0; i<n; i++){
51+
cout << arr[i] << " ";
52+
}
53+
cout << endl;
54+
}
55+
return 0;
56+
}
57+
58+
59+
// } Driver Code Ends

0 commit comments

Comments
 (0)