-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversearrayingroup.cpp
More file actions
45 lines (36 loc) · 918 Bytes
/
reversearrayingroup.cpp
File metadata and controls
45 lines (36 loc) · 918 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void reverseInGroups(vector<long long>& arr, int n, int k) {
for (int i = 0; i < n; i += k) {
int left = i;
int right = min(i + k - 1, n - 1);
while (left < right) {
swap(arr[left], arr[right]);
left++;
right--;
}
}
}
};
// Utility function to print the array
void printArray(const vector<long long>& arr) {
for (long long num : arr) {
cout << num << " ";
}
cout << endl;
}
int main() {
vector<long long> arr = {1, 2, 3, 4, 5, 6, 7, 8};
int n = arr.size();
int k = 3;
cout << "Original array: ";
printArray(arr);
Solution sol;
sol.reverseInGroups(arr, n, k);
cout << "Array after reversing in groups: ";
printArray(arr);
return 0;
}