82. Zig-Zag Arrangement
MediumGreedy
Rearrange the array so that a₁ < a₂ > a₃ < a₄ > … using this rule: scan left to right and swap a[i] and a[i+1] whenever the pair breaks the pattern. Print the result. All values are distinct.
Input format
Line 1: n Line 2: n distinct integers
Output format
The rearranged array.
Example 1
Input
7 4 3 7 8 6 2 1
Output
3 7 4 8 2 6 1
Example 2
Input
4 1 4 3 2
Output
1 4 2 3
Constraints
1 ≤ n ≤ 10⁵
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…