#K50722. Reversing One Segment to Sort an Array

    ID: 28928 Type: Default 1000ms 256MiB

Reversing One Segment to Sort an Array

Reversing One Segment to Sort an Array

You are given an array of n integers. You need to determine if it is possible to sort the array in non-decreasing order by reversing exactly one contiguous segment of the array.

In other words, you should check if there exist indices l and r (with \(1 \leq l \leq r \leq n\)) such that if you reverse the subarray between indices l and r, the whole array becomes sorted in non-decreasing order. Otherwise, output that it is not possible.

Note: If the array is already sorted, then no reversal is needed and the answer is considered "YES".

The key idea is to identify the segment where the array is not in non-decreasing order, reverse it, and then check if the resulting array is sorted.

The array indices here are considered 1-based when describing the segment, but input processing will be zero-based in your implementation.

inputFormat

The first line contains a single integer n (1 \(\leq\) n \(\leq\) 105), the number of elements in the array.

The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single line containing "YES" if the array can be sorted by reversing exactly one contiguous segment, and "NO" otherwise.

## sample
5
10 20 30 40 50
YES

</p>