#C6092. Zig-Zag Sequence Detector
Zig-Zag Sequence Detector
Zig-Zag Sequence Detector
You are given a sequence of integers. A sequence is called a Zig-Zag sequence if for every index i (with 1 ≤ i ≤ n-2), the element at index i is either strictly greater than both of its neighbors or strictly less than both of its neighbors. In mathematical terms, for a sequence \(a_0, a_1, \dots, a_{n-1}\), for every \(1 \leq i \leq n-2\), one of the following must hold:
[ \left{ \begin{array}{l} a_i > a_{i-1} \quad \text{and} \quad a_i > a_{i+1}, \ a_i < a_{i-1} \quad \text{and} \quad a_i < a_{i+1}. \end{array} \right. ]
Your task is to determine whether each given sequence is a valid Zig-Zag sequence. If the sequence contains one or no elements, it is trivially considered a Zig-Zag sequence.
inputFormat
The input is read from standard input and is organized as follows:
- The first line contains a single integer T, representing the number of test cases.
- For each test case, there are two lines:
- The first line contains an integer N, the number of elements in the sequence.
- The second line contains N integers separated by spaces representing the sequence.
outputFormat
For each test case, output a single line with either "YES" if the sequence is a valid Zig-Zag sequence, or "NO" otherwise. The output is written to standard output.
## sample3
5
1 3 2 4 3
6
10 5 15 10 20 5
4
1 2 2 1
YES
YES
NO
</p>