#C3767. Minimize Maximum Difference
Minimize Maximum Difference
Minimize Maximum Difference
You are given an array of n integers. Your task is to rearrange the array such that the maximum absolute difference between any two consecutive elements is minimized.
In mathematical terms, given an array \(a_1, a_2, \ldots, a_n\), you need to find a permutation \(p\) of the array such that the value \[ \max_{1 \leq i < n} |p_i - p_{i+1}| \quad \] is as small as possible. It turns out that simply sorting the array in non-decreasing order already achieves this goal.
Example:
Input: 5\n10 1 9 7 3 Output: 1 3 7 9 10
inputFormat
The first line contains a single integer n which is the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.
For example:
5 10 1 9 7 3
outputFormat
Output the rearranged array in a single line, with the integers separated by a single space. The arrangement should minimize the maximum absolute difference between consecutive elements, which is achieved by sorting the array in non-decreasing order.
For example:
1 3 7 9 10## sample
5
10 1 9 7 3
1 3 7 9 10