#K65377. Minimal Difference Ordering

    ID: 32184 Type: Default 1000ms 256MiB

Minimal Difference Ordering

Minimal Difference Ordering

You are given an array \( A \) of \( N \) integers. Your task is to construct a new array \( B \) by inserting each element of \( A \) one by one into an initially empty array. The insertion position should be chosen such that the total sum of absolute differences between adjacent elements in \( B \) is minimized. Formally, if \( B = [B_1, B_2, \dots, B_N] \) then you want to minimize:

[ \sum_{i=1}^{N-1} |B_i - B_{i+1}|, ]

It turns out that the optimal solution is to arrange the array in non-decreasing order. You need to implement this ordering and output the final array in a space-separated format.

Note: The input consists of multiple test cases. For each test case, first an integer \( N \) (the size of the array) is given, followed by \( N \) integers representing the array \( A \). The output for each test case should be the optimally ordered array (i.e., the sorted array).

inputFormat

The first line of the input contains a single integer \( T \) denoting the number of test cases. For each test case:

  1. The first line contains an integer \( N \) (the number of elements in the array).
  2. The second line contains \( N \) space-separated integers representing the array \( A \).

Read input from standard input (stdin).

outputFormat

For each test case, output a single line containing the elements of the optimally ordered array (i.e., the sorted array) in a space-separated format.

Write output to standard output (stdout).

## sample
2
3
1 3 5
4
10 20 30 40
1 3 5

10 20 30 40

</p>