#C9209. Minimize Absolute Difference
Minimize Absolute Difference
Minimize Absolute Difference
You are given an integer n and an array of n integers. Your task is to rearrange the array such that the absolute difference between any two adjacent elements is minimized. In other words, you are required to minimize the quantity
which is naturally achieved by outputting the array in non-decreasing order.
Example: For the input array [3, 2, 1, 4], the rearranged order should be [1, 2, 3, 4].
inputFormat
The first line contains a single integer n
representing the number of elements in the array. The second line contains n
space-separated integers which constitute the array.
outputFormat
Output a single line containing the rearranged array, where the elements are sorted in non-decreasing order and separated by a single space.
## sample4
3 2 1 4
1 2 3 4