#K4066. Rearranging Array with No Adjacent Difference of One

    ID: 26692 Type: Default 1000ms 256MiB

Rearranging Array with No Adjacent Difference of One

Rearranging Array with No Adjacent Difference of One

You are given an integer array. Your task is to determine whether it is possible to rearrange the array such that for every adjacent pair \(a_i\) and \(a_{i+1}\), the condition
\( |a_{i+1} - a_i| \neq 1 \) holds.

If a valid rearrangement exists, you should output YES followed by the rearranged array elements (space-separated). If not, output NO.

Note: The output rearrangement does not need to be unique. In this problem, the solution strategy is to sort the array and then check if any two consecutive elements differ by exactly 1. If such a difference exists, the answer is considered NO; otherwise, the sorted order is output as a valid rearrangement.

inputFormat

The input begins with an integer \(T\) (the number of test cases). For each test case, the first line contains an integer \(n\) denoting the number of elements in the array, and the second line contains \(n\) space-separated integers representing the array elements.

outputFormat

For each test case, output the result on a new line. If a valid rearrangement exists, output YES followed by the rearranged array (elements separated by spaces). Otherwise, output NO.

## sample
3
5
1 2 3 4 5
6
10 20 30 40 50 60
3
2 1 3
NO

YES 10 20 30 40 50 60 NO

</p>