#K6276. Lexicographically Smallest Sorted Array for Minimal Absolute Differences
Lexicographically Smallest Sorted Array for Minimal Absolute Differences
Lexicographically Smallest Sorted Array for Minimal Absolute Differences
You are given one or more test cases. In each test case, you are provided with an array of integers. Your task is to rearrange the array elements such that the sum of absolute differences between consecutive elements is minimized. It can be proved that the arrangement that minimizes the sum \(\sum_{i=1}^{n-1} |a_{i+1} - a_i|\) is obtained by sorting the array in non-decreasing order. In the event of multiple solutions, you are required to output the lexicographically smallest sorted array.
Note: Each test case starts with an integer \(n\) indicating the number of elements in the array, followed by \(n\) integers.
inputFormat
The first line of input contains an integer \(T\) representing the number of test cases. Each of the following \(T\) lines describes a test case. For each test case, the line starts with an integer \(n\) (the number of elements in the array) followed by \(n\) space-separated integers.
Example:
2 3 1 5 3 4 8 3 6 1
outputFormat
For each test case, print a single line containing the sorted (non-decreasing) array such that the sum of absolute differences between consecutive elements is minimized.
Example:
1 3 5 1 3 6 8## sample
2
3 1 5 3
4 8 3 6 1
1 3 5
1 3 6 8
</p>