#K71927. Zigzag Array
Zigzag Array
Zigzag Array
You are given an array of n integers. Your task is to determine whether the array forms a zigzag pattern. An array is considered to be in zigzag order if and only if it contains exactly one peak element such that:
- The element at index i (with 1 ≤ i ≤ n-2) is greater than both of its neighbors (i.e. a[i] > a[i-1] and a[i] > a[i+1]).
- The part of the array before the peak must be strictly increasing.
- The part of the array after the peak must be strictly decreasing.
Note that if n < 3, the array cannot form a zigzag pattern. In such cases, the answer is No
.
More formally, if there exists an index i with 1 ≤ i ≤ n-2 such that:
$$ a[i] > a[i-1] \quad \text{and} \quad a[i] > a[i+1], $$and for this index:
- For every j with 1 ≤ j < i,
a[j] > a[j-1]
. - For every j with i+1 ≤ j < n,
a[j] < a[j-1]
.
Then, output Yes
. Otherwise, output No
for that test case.
inputFormat
The input consists of multiple test cases. Each test case is given on a separate line. Each test case starts with an integer n (the number of elements in the array) followed by n integers representing the array elements. The input terminates with a line containing a single 0
, which should not be processed.
outputFormat
For each test case, output a single line with Yes
if the array is a zigzag array, otherwise No
.
5 2 5 7 6 3
0
Yes