#C3215. Alternating Student Heights Arrangement
Alternating Student Heights Arrangement
Alternating Student Heights Arrangement
You are given a list of n students' heights. Your task is to rearrange the heights such that the heights of neighboring students alternate between increasing and decreasing. Formally, if the rearranged array is \(a_0, a_1, \ldots, a_{n-1}\), then for every index \(i\) (where applicable) it should satisfy:
\(a_{i} a_{i+1}\) if \(i\) is odd.
If \(n = 0\) or \(n = 1\), simply output the original array.
It is guaranteed that an answer always exists using the following approach: sort the array and then pick the smallest remaining element for even positions and the largest remaining element for odd positions.
Example: For an input of 4 3 2 1 6
(i.e. 5 students), one possible valid output is 1 6 2 4 3
.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (n) — the number of students. The second line contains (n) space-separated integers representing the heights of the students.
outputFormat
Print the rearranged list of heights as space-separated integers on a single line to standard output (stdout).## sample
5
4 3 2 1 6
1 6 2 4 3