#C10544. Sort the Array by Reversing a Subsegment

    ID: 39761 Type: Default 1000ms 256MiB

Sort the Array by Reversing a Subsegment

Sort the Array by Reversing a Subsegment

You are given an array of n integers. Your task is to determine whether it is possible to sort the array in non-decreasing order by reversing exactly one contiguous subsegment. In mathematical terms, given an array (a_1, a_2, \dots, a_n), check if there exists indices (l) and (r) ((1 \leq l \leq r \leq n)) such that if we reverse the segment (a_l, a_{l+1}, \dots, a_r) the resulting array is sorted in non-decreasing order.

Note: If the array is already sorted, the answer is YES.

inputFormat

The input is read from stdin and consists of two lines. The first line contains a single integer (n) ((1 \leq n \leq 10^5)), representing the number of elements in the array. The second line contains (n) space-separated integers representing the array elements.

outputFormat

Output to stdout a single line containing YES if there exists a subsegment that, when reversed, sorts the array in non-decreasing order. Otherwise, output NO.## sample

5
1 3 5 4 6
YES