#C5931. Special Sequence Transformation
Special Sequence Transformation
Special Sequence Transformation
You are given an array of n integers. The task is to determine whether it is possible to rearrange the array into a special sequence where the elements alternately increase and decrease. A necessary and sufficient condition for such a transformation is that all elements in the array are distinct.
If the transformation is possible, output YES
; otherwise, output NO
.
Note: The alternating increase and decrease pattern can always be achieved if all the numbers are unique.
For example, the array [1, 3, 5, 2, 4]
is transformable (YES
), whereas [4, 5, 5, 6]
is not (NO
).
inputFormat
The input begins with an integer T (the number of test cases). Each test case consists of the following:
- 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, output a single line with YES
if the array can be transformed into a special sequence, and NO
otherwise.
3
5
1 3 5 2 4
4
4 5 5 6
1
1
YES
NO
YES
</p>