IBMOA #53
Meandering Array
Estimated EasyJudge-ready
Topics (AI-labelled)In-place ArraysTwo Pointers
Output all array elements in the order largest, smallest, second largest, second smallest, and so on, keeping duplicates and using each input element exactly once.
Input
The first line contains n, and the second line contains the array. 2≤n≤100000, −10^6≤element≤10^6, and duplicates are allowed.
Output
Output n elements arranged in alternating maximum-minimum order.
Example 1
Input:
5
-1 1 2 3 -5
Output:
3 -5 2 -1 1
Example 2
Input:
3
2 2 1
Output:
2 1 2
Example 3
Input:
2
0 0
Output:
0 0
Example notes
The examples give 3 -5 2 -1 1; 2 1 2; and 0 0 respectively.
Editorial
Loading solution…