#C1580. Minimize Consecutive Differences
Minimize Consecutive Differences
Minimize Consecutive Differences
In this problem, you are given multiple arrays. For each array, your task is to rearrange its elements so that the difference between any two consecutive elements is minimized. In simple terms, you must sort each array in non-decreasing order. This is because sorting guarantees that the absolute difference between consecutive elements is as small as possible.
Formally, for an array (a_1,a_2,\dots,a_n), you need to find a permutation (b_1,b_2,\dots,b_n) of the array such that (\max_{1\le i < n} |b_{i+1} - b_{i}|) is minimized. It can be shown that sorting the array is an optimal solution.
Input and output are handled via standard input and output.
inputFormat
The first line of input contains a single integer (T) denoting the number of test cases. For each test case, the first line contains an integer (N) representing the number of elements in the array. The second line contains (N) space-separated integers representing the elements of the array.
outputFormat
For each test case, output a single line containing (N) space-separated integers: the rearranged array such that the difference between any two consecutive elements is minimized (i.e. sorted in non-decreasing order).## sample
6
4
4 2 1 3
5
10 5 15 10 20
3
3 2 1
4
1 1 1 1
3
-1 -2 -3
4
1000000000 -1000000000 0 5
1 2 3 4
5 10 10 15 20
1 2 3
1 1 1 1
-3 -2 -1
-1000000000 0 5 1000000000
</p>