-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathzig-zag-sequence.cpp
More file actions
44 lines (39 loc) · 916 Bytes
/
Copy pathzig-zag-sequence.cpp
File metadata and controls
44 lines (39 loc) · 916 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
// Algorithms > Debugging > Zig Zag Sequence
// Find a zig zag sequence of the given array.
//
// https://www.hackerrank.com/challenges/zig-zag-sequence/problem
// challenge id: 63282
//
#include <bits/stdc++.h>
using namespace std;
void findZigZagSequence(vector < int > a, int n){
sort(a.begin(), a.end());
int mid = (n - 1)/2;
swap(a[mid], a[n-1]);
int st = mid + 1;
int ed = n - 2;
while(st <= ed){
swap(a[st], a[ed]);
st = st + 1;
ed = ed - 1;
}
for(int i = 0; i < n; i++){
if(i > 0) cout << " ";
cout << a[i];
}
cout << endl;
}
int main() {
int n, x;
int test_cases;
cin >> test_cases;
for(int cs = 1; cs <= test_cases; cs++){
cin >> n;
vector < int > a;
for(int i = 0; i < n; i++){
cin >> x;
a.push_back(x);
}
findZigZagSequence(a, n);
}
}