Skip to content

Commit 9d5c1a6

Browse files
authored
Merge pull request #79 from ranjitmnair/patch-1
Create ZigZagSequence.cpp
2 parents 1b9100e + 89b6d34 commit 9d5c1a6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void findZigZagSequence(vector < int > a, int n){
5+
sort(a.begin(), a.end());
6+
int mid = (n + 1)/2;
7+
swap(a[mid], a[n-1]);
8+
9+
int st = mid + 1;
10+
int ed = n - 1;
11+
while(st <= ed){
12+
swap(a[st], a[ed]);
13+
st = st + 1;
14+
ed = ed + 1;
15+
}
16+
for(int i = 0; i < n; i++){
17+
if(i > 0) cout << " ";
18+
cout << a[i];
19+
}
20+
cout << endl;
21+
}
22+
23+
int main() {
24+
int n, x;
25+
int test_cases;
26+
cin >> test_cases;
27+
28+
for(int cs = 1; cs <= test_cases; cs++){
29+
cin >> n;
30+
vector < int > a;
31+
for(int i = 0; i < n; i++){
32+
cin >> x;
33+
a.push_back(x);
34+
}
35+
findZigZagSequence(a, n);
36+
}
37+
}
38+
39+
40+

0 commit comments

Comments
 (0)