#K59887. Reordering Teams to Avoid Adjacent Identical Points
Reordering Teams to Avoid Adjacent Identical Points
Reordering Teams to Avoid Adjacent Identical Points
You are given several test cases. In each test case, you have n teams with assigned points. Your task is to determine if it is possible to rearrange the teams so that no two adjacent teams have the same number of points.
It can be shown that a necessary and sufficient condition for such an arrangement to exist is that the maximum frequency of any point value does not exceed \( \lfloor \frac{n+1}{2} \rfloor \). In other words, if \(f_{max}\) denotes the maximum number of occurrences of any point value, then the reordering is possible if and only if:
[ f_{max} \leq \left\lfloor \frac{n+1}{2} \right\rfloor ]
For each test case, output YES
if such a reordering is possible, and NO
otherwise.
inputFormat
The input is read from stdin and has the following format:
- An integer
T
representing the number of test cases. - For each test case:
- An integer
n
representing the number of teams. - A line with
n
space-separated integers indicating the points of each team.
- An integer
It is guaranteed that the total number of teams in all test cases does not exceed reasonable limits.
outputFormat
For each test case, output a single line to stdout containing either YES
or NO
. YES
indicates that it is possible to rearrange the teams such that no two adjacent teams have the same number of points, while NO
indicates that it is impossible.
3
5
4 3 3 4 2
4
1 1 1 1
3
7 6 6
YES
NO
YES
</p>