#C8554. Mountain-Like Sequence Rearrangement
Mountain-Like Sequence Rearrangement
Mountain-Like Sequence Rearrangement
You are given an integer n and a sequence of n integers. Your task is to determine whether it is possible to rearrange the elements of the sequence into a mountain-like sequence. A mountain-like sequence is defined such that there exists an index i (with $1 \lt i \lt n$) which serves as a peak of the sequence. In other words, the sequence first strictly increases to a unique maximum element and then strictly decreases.
Note: For the purpose of this problem, if n is less than 3, it is impossible to form a mountain-like sequence. In addition, if the maximum element appears more than once, a unique peak cannot be formed, and thus the answer should be "NO".
The input format is as follows: the first line contains an integer n and the second line contains the sequence of n space-separated integers.
The output should be a single line with either "YES" if it is possible to form a mountain-like sequence or "NO" otherwise.
Formally, for the sequence to be mountain-like, the following conditions must hold:
- $n \geq 3$
- Let $a_1, a_2, \dots, a_n$ be the sequence after rearrangement. There exists an index $i$ with $1 < i < n$ such that
$a_1 < a_2 < \dots \lt a_i$ and $a_i > a_{i+1} > \dots > a_n$. - The element $a_i$ (the peak) must be unique.
It can be shown that checking whether the maximum element is unique (after sorting) is a sufficient condition for this problem.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains a single integer n ($1 \leq n \leq 10^5$), which is the number of elements in the sequence.
- The second line contains n space-separated integers representing the sequence.
outputFormat
Output a single line: YES
if it is possible to rearrange the sequence into a mountain-like sequence, or NO
otherwise.
5
3 5 2 1 4
YES