All problems

Problem 811

Valid Mountain Array

Easy · 10 pts · Array

Problem

An array is a mountain if it has at least 3 elements and there is a peak index i (not the first or the last) such that the elements strictly increase up to A[i] and strictly decrease after it. Equal neighbours are not allowed anywhere, and an array that only goes up or only goes down is not a mountain. Walk up while the next element is larger, then down while it is smaller, and check that you stopped at the end and that both parts were non-empty.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print YES if the array is a mountain, otherwise NO.

Constraints

1 <= N <= 100000; 0 <= A[i] <= 10^4

Example input

4
0 3 2 1

Example output

YES

Explanation

0 3 2 1 rises from 0 to 3 and then falls to 2 and 1, with the peak 3 in the middle, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Monotonic Array Next: Sum of All Odd Length Subarrays