A refinery control room logs a storage tank's pressure once every minute for an entire shift, producing a sequence of n integer readings in the order they were taken. Plant engineers only auto-approve a shift's log when the pressure moved consistently in one direction the whole time -- that is, the readings never decreased at any point (a non-decreasing trend) or never increased at any point (a non-increasing trend); staying flat for a while is fine either way. Given the shift's log, decide whether it qualifies for auto-approval.
The first line contains a single integer n, the number of readings.
The second line contains n space-separated integers p[0], p[1], ..., p[n-1], the pressure readings in chronological order.
Print YES if the sequence is entirely non-decreasing or entirely non-increasing, and NO otherwise.
Example 1
Input
6 1 2 2 3 5 5
Expected
YES
Explanation
Every consecutive reading is greater than or equal to the previous one (1<=2<=2<=3<=5<=5), so the shift never lost pressure -- a non-decreasing trend -- and the log is approved, printing YES.
Example 2
Input
5 5 3 4 2 1
Expected
NO
Explanation
The pressure drops from 5 to 3, but then rises from 3 to 4 before falling again to 2 and 1. Because it neither stays non-decreasing throughout (it drops early on) nor non-increasing throughout (it rises from 3 to 4), the trend is inconsistent and the log is rejected, printing NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →