#K84257. Maximize Absolute Difference Permutation
Maximize Absolute Difference Permutation
Maximize Absolute Difference Permutation
You are given an array of N integers. Your task is to rearrange (i.e., permute) the array such that the sum of the absolute differences of adjacent elements is maximized.
One way to achieve this is to first sort the array and then alternately select the smallest and largest remaining elements. Formally, if the sorted array is \(a_1 \le a_2 \le \cdots \le a_N\), a valid permutation can be constructed by selecting \(a_1, a_N, a_2, a_{N-1}, \ldots\). In some cases, a slight adjustment may be needed to further maximize the result.
If there are multiple answers, output any one of them.
inputFormat
The first line contains a single integer (N) representing the number of elements. The second line contains (N) space-separated integers representing the array.
outputFormat
Output a permutation of the given array that maximizes the sum of the absolute differences between consecutive elements. The numbers should be printed in a single line separated by spaces.## sample
3
1 2 3
1 3 2