#K45757. Minimize Absolute Differences
Minimize Absolute Differences
Minimize Absolute Differences
Given several arrays, rearrange each array such that the sum of the absolute differences between adjacent elements is minimized. Formally, for an array (a_1, a_2, \dots, a_n), you need to minimize (\sum_{i=1}^{n-1} |a_{i+1} - a_i|). It can be shown that sorting the array in non-decreasing order achieves this minimum.
The input consists of (T) test cases. For each test case, you are given an integer (N) (the size of the array) followed by (N) integers. For each test case, output the sorted array on a separate line with the values separated by spaces.
inputFormat
The input is read from standard input (stdin). The first line contains a single integer (T) denoting the number of test cases. For each test case, the first line contains an integer (N), the number of elements in the array. The next line contains (N) space-separated integers.
outputFormat
For each test case, output a single line containing the reordered (sorted) array, with the elements separated by a single space. The output is printed to standard output (stdout).## sample
3
4
4 1 2 3
3
10 50 20
5
5 1 3 4 2
1 2 3 4
10 20 50
1 2 3 4 5
</p>