#C3018. Rearrangement for Subsequence Distinctiveness

    ID: 46399 Type: Default 1000ms 256MiB

Rearrangement for Subsequence Distinctiveness

Rearrangement for Subsequence Distinctiveness

In this problem, you are given an array of integers and must determine whether it is possible to rearrange the array so that every contiguous subsequence of length 3 contains only distinct elements. Formally, for an array of length (n), no integer should appear more than (n - \lfloor n/3 \rfloor) times. If a valid rearrangement exists, output "YES" on one line followed by the rearranged array (the original order is acceptable if the condition is satisfied) on the next line. Otherwise, output "NO".

inputFormat

Input is given via standard input. The first line contains an integer (T) denoting the number of test cases. Each test case consists of two lines: the first line contains an integer (n) (the size of the array), and the second line contains (n) space-separated integers representing the array elements.

outputFormat

For each test case, if it is impossible to rearrange the array to satisfy the condition, print "NO". If it is possible, print "YES" on one line and then print the array (elements separated by a single space) on the next line. Each test case's output should follow immediately after the previous one.## sample

3
5
4 5 5 5 4
4
3 3 3 3
6
7 8 7 8 7 8
YES

4 5 5 5 4 NO YES 7 8 7 8 7 8

</p>