-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrearrangeArray.cpp
46 lines (38 loc) · 951 Bytes
/
rearrangeArray.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
#include <iostream>
using namespace std;
void rearrangeArray(int array[], int n) {
for (int i = 0; i < n - 1; ++i) {
if (i % 2 == 0) {
if (array[i] > array[i + 1]) {
swap(array[i], array[i + 1]);
}
}
else {
if (array[i] < array[i + 1]) {
swap(array[i], array[i + 1]);
}
}
}
}
int main() {
int cases;
cout << "Enter the number of cases : ";
cin >> cases;
while (cases--) {
int size;
cout << "Enter the size : ";
cin >> size;
int array[size];
for (int i = 0; i < size; ++i) {
cout << "Enter the element no. #" << (i + 1) << " : ";
cin >> array[i];
}
rearrangeArray(array, size);
cout << "Output : ";
for (int i : array) {
cout << '[' << i << ']';
}
cout << endl;
}
return 0;
}