#C9654. Minimize Adjacent Differences
Minimize Adjacent Differences
Minimize Adjacent Differences
You are given an array of k integers. Your task is to rearrange (or sort) the array in such a way that the average difference between consecutive elements is minimized.
Formally, given an array \( A = [a_1, a_2, \dots, a_k] \), you need to output a permutation \( P = [p_1, p_2, \dots, p_k] \) of \( A \) such that the value
\[ \frac{1}{k-1}\sum_{i=1}^{k-1} |p_{i+1} - p_i| \]is minimized. It can be proved that the sorted order of the array minimizes this average difference.
Note: The integer k provided in the input represents the number of elements in the array.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer \( k \) representing the number of elements in the array.
- The second line contains \( k \) space-separated integers representing the elements of the array.
outputFormat
Print the rearranged array to stdout in a single line with elements separated by spaces.
## sample5
1 3 7 8 5
1 3 5 7 8
</p>