616. Up Then Down, and Never Flat
An array is a MOUNTAIN when it rises strictly to a single peak and then falls strictly, with at least one step on each side. So the peak is neither the first nor the last element, and no two neighbours are ever equal.
Print Yes or No.
0 3 2 1 -> Yes
1 2 3 -> No it never comes down
3 5 5 -> No the peak is flat
## Walk up, then walk down, then check where you stopped
i = 0
while i+1 < n and a[i] < a[i+1]: i += 1 climb
if i == 0 or i == n-1: answer No
while i+1 < n and a[i] > a[i+1]: i += 1 descend
answer Yes if i == n-1 else No
Both loops use STRICT comparisons, so a flat step stops the climb and also stops the descent.
That single choice is what rejects 3 5 5 and 1 1 1 without any separate test.
## The three ways it fails, and the one line that catches each
· i == 0: it never rose, so there is no left slope. 3 2 1 fails here.
· i == n-1 after climbing: it rose all the way to the end, so there is no right slope.
1 2 3 fails here.
· i != n-1 after descending: the descent stopped early, which means a flat step or a
second rise. 3 5 5 and 1 3 2 3 fail here.
Checking the first two BEFORE the descent matters. On 1 2 3 the descent loop does nothing,
so without the early check the final test would pass i == n-1 and answer Yes.
## Why an array shorter than three always fails
A mountain needs at least one step up and one step down, so at least three elements. The two
early checks handle it without a length test: with n = 2, either i is 0 or i is
n-1 = 1.
## Boundaries
0 1 0 is the shortest mountain. 5 alone, 2 1, and 1 1 1 all answer No.
## Cost
Time O(n) with a single pass. Space O(1).
Constraints - `1 ≤ n ≤ 200000` - `-10^9 ≤ each value ≤ 10^9`
Input
The first line contains an integer n.
The second line contains n integers.
Output
Print Yes or No on one line.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.