#C565. Mountain Array Formation
Mountain Array Formation
Mountain Array Formation
You are given an array of integers representing the heights of a set of plants. Your task is to determine whether it is possible to rearrange these plants such that they form a mountain array. A mountain array is defined as an array where the elements strictly increase to a unique maximum (the peak) and then strictly decrease. In other words, there exists an index i (1 ≤ i ≤ n-2) such that:
[ a_0 < a_1 < \cdots < a_i \quad\text{and}\quad a_i > a_{i+1} > \cdots > a_{n-1} ]
Note that:
- The array must contain at least 3 elements.
- All elements must be distinct since a repeated value would break the strict increase or decrease condition.
If both conditions are satisfied, then it is always possible to form a mountain array simply by choosing the maximum element as the peak and arranging the remaining elements in increasing order to its left and decreasing order to its right.
Given the heights, output YES if a mountain arrangement is possible, otherwise output NO.
inputFormat
The first line contains an integer T
denoting the number of test cases.
For each test case, the first line contains an integer N
representing the number of plants. The second line contains N
space-separated integers representing the heights of the plants.
outputFormat
For each test case, output a single line with either YES
or NO
(without quotes). Output YES
if it is possible to rearrange the plants to form a mountain array; otherwise, output NO
.
4
5
1 3 5 4 2
4
3 2 1 4
3
1 2 2
5
1 1 2 3
YES
YES
NO
NO
</p>