


Input: arr[] = {1,1,1,2,2,2}
Output: {1,2,1,2,1,2}
Note: {2,1,2,1,2,1} is also valid because there are no two adjacent elements which are the same.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains an Integer 'N' denoting the size of the array/list.
The second line of each test case contains 'N' space-separated Integers denoting the elements of the array/list.
For each test case/query, If it is possible to rearrange then print “YES” else print “NO” in separate lines.
You do not need to print anything; it has already been taken care of.
1 <= T <= 100
1 <= N <= 5000
-10^9 <= NUM[i] <= 10^9
Where 'N' is the size of the given array/list and, NUM[i] denotes the i-th element in the array/list.
The key idea is to put the highest frequency element first (a greedy approach). We will be using the priority queue to pick the elements with the highest frequency.
So first, we will store the frequency of each element. And then we will store all the numbers with their frequencies into the priority queue in the highest frequency priority manner.
After that, we will pop elements one by one from the priority queue and add them to result only when the current element is not the same as the most recent element saved in the result. Also, we will decrease the frequency of each element and push it back into the priority queue if the element has a frequency greater than '0'.
Here is the complete algorithm: