#K661. Rearranging Grades
Rearranging Grades
Rearranging Grades
You are given several test cases. In each test case, you have an integer n and a list of n integers representing grades. Your task is to determine if it is possible to rearrange the grades such that after sorting they satisfy the condition
\(a_{i+1} - a_i \le 1\)
for every consecutive pair \( (a_i, a_{i+1}) \). In other words, if you sort the grades and denote the sorted grades by \(a_1, a_2, \dots, a_n\), the necessary and sufficient condition is that for all \(i = 1, 2, \dots, n-1\), the difference between \(a_{i+1}\) and \(a_i\) does not exceed 1.
This problem involves careful sorting and checking adjacent differences.
inputFormat
The input is read from standard input (stdin). The first line contains a single integer T denoting the number of test cases. For each test case, there are two lines:
- The first line contains an integer n which represents the number of grades.
- The second line contains n space-separated integers representing the grades.
outputFormat
For each test case, output a single line to standard output (stdout) containing "YES" if it is possible to rearrange the grades to satisfy the condition, or "NO" otherwise.
## sample3
4
1 3 2 2
3
5 5 5
5
8 7 6 5 4
YES
YES
YES
</p>