#B4194. Minimize Maximum Adjacent Height Difference in a Circle
Minimize Maximum Adjacent Height Difference in a Circle
Minimize Maximum Adjacent Height Difference in a Circle
TaoTao is celebrating his birthday with n friends who stand in a circle. Each friend is assigned a unique index from 1 to n and has a height ai for 1 ≤ i ≤ n. The friends want to reorder themselves in the circle so that the maximum absolute difference in heights between any two adjacent friends (with the last and first also considered adjacent) is minimized.
Input: The first line contains an integer n, the number of friends. The second line contains n integers a1, a2, ..., an denoting the heights.
Output: Output one line with a permutation of the given heights (separated by spaces) corresponding to an order around the circle which minimizes the maximum adjacent difference. In other words, if the output order is b1, b2, ..., bn, you should minimize $$\max\{|b_1-b_2|,|b_2-b_3|,\ldots,|b_{n-1}-b_n|,|b_n-b_1|\}.$$
inputFormat
The first line contains an integer n (n ≥ 1).
The second line contains n space-separated integers representing the heights.
outputFormat
Output a single line containing n space-separated integers which represent a circular ordering of the friends that minimizes the maximum absolute difference between adjacent heights.
sample
5
1 2 3 4 5
4 2 1 3 5