Problem 810
Monotonic Array
Easy · 10 pts · Array
Problem
An array is monotonic if it is entirely non-decreasing (each element is at least the one before it) or entirely non-increasing (each element is at most the one before it). Equal neighbours are allowed in both cases, and an array with one element is monotonic. Decide whether the given array is monotonic by checking in one pass whether any step goes up and whether any step goes down.
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 monotonic, otherwise NO.
Constraints
1 <= N <= 100000; -10^9 <= A[i] <= 10^9
Example input
4 1 2 2 3
Example output
YES
Explanation
In 1 2 2 3 every step stays the same or goes up, so the array is non-decreasing and the answer is YES. 1 3 2 goes up and then down, so it would be NO.