#K971. Well-Ordered Subsequence
Well-Ordered Subsequence
Well-Ordered Subsequence
Given an array b
of integers, your task is to determine whether there exists a subsequence that is well-ordered. A well-ordered subsequence satisfies the following:
- The first element is equal to the global minimum of the original array.
- The last element is equal to the global maximum of the original array.
- The subsequence is non-decreasing (i.e. each element is not smaller than the previous one).
Note that the subsequence must contain at least two elements. If the minimum and maximum are identical (i.e. all elements are the same), then a well-ordered subsequence does not exist.
Example:
Input: [1, 5, 3, 2, 4] Output: YES</p>Explanation: One possible well-ordered subsequence is [1, 3, 4] where 1 is the minimum and 4 is the maximum and the sequence is non-decreasing.
inputFormat
The first line contains an integer T
representing the number of test cases. Each test case consists of two lines:
- The first line of a test case contains an integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers.
Input is provided via standard input (stdin).
outputFormat
For each test case, output a single line with either YES
if a well-ordered subsequence exists or NO
otherwise. Output is printed to standard output (stdout).
3
5
1 5 3 2 4
6
6 5 4 3 2 1
7
2 3 4 1 5 6 7
YES
NO
YES
</p>