#C1089. Rearrange Arrays

    ID: 40144 Type: Default 1000ms 256MiB

Rearrange Arrays

Rearrange Arrays

Given multiple test cases, each containing an array of integers, your task is to rearrange each array such that the difference between any two adjacent elements is minimized. In other words, you should output the lexicographically smallest arrangement among all possible arrangements that minimize the adjacent differences. The simplest way to achieve this is to sort the array.

Note: When sorting the array, ensure that the solution is the lexicographically smallest one if there are duplicates or multiple valid arrangements.

Mathematical Note: If A = [a_1, a_2, ..., a_n] is the given array, then after sorting the array we get a rearrangement A' = [a'_{1}, a'_{2}, ..., a'_{n}] such that

$$|a'_{i+1} - a'_{i}| \leq |a'_{j+1} - a'_{j}|,\ \ \forall\ i, j $$

This guarantees that adjacent differences are as small as possible.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains an integer T, the number of test cases.
  2. For each test case, the first line contains an integer N, the number of elements in the array.
  3. The second line contains N integers separated by spaces.

For example:

1
4
4 3 1 2

outputFormat

For each test case, output the rearranged array on a separate line. The numbers in the array should be separated by a single space. The output is printed to standard output (stdout).

For example, the output for the test case above is:

1 2 3 4
## sample
1
4
4 3 1 2
1 2 3 4