CF BUDDY
← Problems·

1685A · Circular Local MiniMax

1100 · constructive algorithms, greedy, sortings

Problem: You are given nn integers a1,a2,,ana_1, a_2, \ldots, a_n. Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors?

In other words, check if there exists a rearrangement b1,b2,,bnb_1, b_2, \ldots, b_n of the integers a1,a2,,ana_1, a_2, \ldots, a_n such that for each ii from 11 to nn at least one of the following conditions holds:

  • bi1<bi>bi+1b_{i-1} < b_i > b_{i+1}
  • bi1>bi<bi+1b_{i-1} > b_i < b_{i+1}

To make sense of the previous formulas for i=1i=1 and i=ni=n, one shall define b0=bnb_0=b_n and bn+1=b1b_{n+1}=b_1.

Input Format: The first line of the input contains a single integer tt (1t31041 \le t \le 3\cdot 10^4)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (3n1053 \le n \le 10^5)  — the number of integers.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai1090 \le a_i \le 10^9).

The sum of nn over all test cases doesn't exceed 21052\cdot 10^5.

Output Format: For each test case, if it is not possible to arrange the numbers on the circle satisfying the conditions from the statement, output NO\texttt{NO}. You can output each letter in any case.

Otherwise, output YES\texttt{YES}. In the second line, output nn integers b1,b2,,bnb_1, b_2, \ldots, b_n, which are a rearrangement of a1,a2,,ana_1, a_2, \ldots, a_n and satisfy the conditions from the statement. If there are multiple valid ways to arrange the numbers, you can output any of them.

Note: It can be shown that there are no valid arrangements for the first and the third test cases.

In the second test case, the arrangement [1,8,4,9][1, 8, 4, 9] works. In this arrangement, 11 and 44 are both smaller than their neighbors, and 8,98, 9 are larger.

In the fourth test case, the arrangement [1,11,1,111,1,1111][1, 11, 1, 111, 1, 1111] works. In this arrangement, the three elements equal to 11 are smaller than their neighbors, while all other elements are larger than their neighbors.

Sample Cases

Case 1

Input

4
3
1 1 2
4
1 9 8 4
4
2 0 2 2
6
1 1 1 11 111 1111

Output

NO
YES
1 8 4 9 
NO
YES
1 11 1 111 1 1111

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback