#C3198. Conditional Sorting

    ID: 46598 Type: Default 1000ms 256MiB

Conditional Sorting

Conditional Sorting

You are given an integer T representing the number of test cases. For each test case, you are given two integers: n and k and an array of n integers. Your task is to partition the array into three parts:

  • All elements strictly less than k
  • All elements equal to k
  • All elements strictly greater than k

The relative order of the elements in each partition must remain the same as in the original array. In other words, if we define the partitions as:

$$A = \{a_i \mid a_i < k \}, \quad B = \{a_i \mid a_i = k \}, \quad C = \{a_i \mid a_i > k \}, $$

then the resulting array is the concatenation A + B + C.

Example:

Input: 6 3
       4 3 5 2 3 1
Output: 2 1 3 3 4 5

It means that the numbers less than 3 come first (2 and 1), followed by numbers equal to 3 (3 and 3), and finally numbers greater than 3 (4 and 5).

inputFormat

The first line of input contains an integer T - the number of test cases. The description of T test cases follows.

For each test case, the first line contains two integers n and k, where n is the number of elements in the array and k is the key used for partitioning. The second line contains n space-separated integers representing the array.

outputFormat

For each test case, output a single line containing the array after partitioning, with the elements separated by a space. The new array should have all elements less than k first (preserving their order), followed by the elements equal to k, and then the elements greater than k.

## sample
1
4 5
6 5 5 4
4 5 5 6