#K75417. Minimize Maximum Difference Sequence
Minimize Maximum Difference Sequence
Minimize Maximum Difference Sequence
You are given a number of dishes, each with an integer representing its flavor profile. Your task is to arrange the dishes in a sequence such that the maximum absolute difference between any two consecutive dishes is minimized. In this problem, it turns out that the optimal solution is to sort the list of dishes in ascending order.
Formally, given an integer ( n ) representing the number of dishes and a list of ( n ) integers ( a_1, a_2, \ldots, a_n ) representing the flavor profiles, output the sequence sorted in non-decreasing order. The sorted sequence minimizes the maximum absolute difference between consecutive elements.
For example, if ( n = 5 ) and the dishes are [4, 8, 6, 9, 2], the optimal sequence is [2, 4, 6, 8, 9].
inputFormat
The input is given via stdin and consists of two lines. The first line contains a single integer ( n ) indicating the number of dishes. The second line contains ( n ) space-separated integers representing the flavor profiles of the dishes.
outputFormat
Output the sorted sequence of dishes (using a single space as a separator) to stdout. This sequence minimizes the maximum absolute difference between consecutive dishes.## sample
5
4 8 6 9 2
2 4 6 8 9