#K49387. Rearrangement for Adjacent Differences
Rearrangement for Adjacent Differences
Rearrangement for Adjacent Differences
You are given T test cases. For each test case, you are provided with an integer N and an array of N integers. Your task is to determine whether it is possible to rearrange the elements of the array such that for every two adjacent integers, the absolute difference is at most 1.
In other words, for a valid rearrangement a1, a2, \ldots, aN, the condition below must hold for every i (2 \le i \le N):
If such an arrangement exists, print POSSIBLE
; otherwise, print IMPOSSIBLE
for that test case.
Note: The most straightforward approach is to sort the array and then check if every adjacent pair satisfies the condition.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer T representing the number of test cases.
- For each test case:
- The first line contains an integer N, the number of elements in the array.
- The second line contains N space-separated integers.
outputFormat
For each test case, print a single line containing POSSIBLE
if a valid rearrangement exists or IMPOSSIBLE
if it does not. The output should be written to stdout.
2
5
3 5 4 4 3
6
1 2 2 5 4 4
POSSIBLE
IMPOSSIBLE
</p>