#K61667. Minimize Absolute Difference
Minimize Absolute Difference
Minimize Absolute Difference
You are given a sequence of integers. Your task is to reorder the sequence so that the absolute difference between any two adjacent integers is minimized. In other words, if you have a sequence \(a_1, a_2, \dots, a_n\), you need to find a permutation of these numbers such that the values \(|a_2 - a_1|, |a_3 - a_2|, \dots, |a_n - a_{n-1}|\) are as small as possible.
The optimal strategy is to sort the sequence in non-decreasing order. For example, if the sequence is [3, -1, 2, -5, 4, 0], the sorted sequence becomes [-5, -1, 0, 2, 3, 4] which minimizes the adjacent differences.
inputFormat
The input is read from standard input (stdin) and has the following structure:
- An integer \(T\) representing the number of test cases.
- For each test case:
- An integer \(n\) representing the number of elements in the sequence.
- A line of \(n\) space-separated integers.
outputFormat
For each test case, output one line containing the sequence reordered such that the absolute differences between adjacent numbers are minimized. The numbers should be printed as space-separated integers on a single line. The output is printed to standard output (stdout).
## sample3
5
1 6 3 9 2
4
10 4 15 12
3
-1 -5 -3
1 2 3 6 9
4 10 12 15
-5 -3 -1
</p>