Problem 701
Check if Array Is Sorted and Rotated
Easy · 10 pts · Array
Problem
An array is a rotated sorted array if it can be obtained by taking an array sorted in non-decreasing order and rotating it some number of positions (possibly zero), so that a suffix moves to the front. Decide whether the given array is one. Duplicates are allowed. Walk around the array in a circle and count the places where a value is followed by a smaller value; the array qualifies exactly when there is at most one such drop.
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 rotated sorted array, otherwise NO.
Constraints
1 <= N <= 100000; 1 <= A[i] <= 10^9
Example input
5 3 4 5 1 2
Example output
YES
Explanation
3 4 5 1 2 is the sorted array 1 2 3 4 5 rotated so that 3 4 5 comes first. Going around in a circle, the only drop is from 5 to 1, so the answer is YES.