#K70187. Sort by Reversing a Subarray
Sort by Reversing a Subarray
Sort by Reversing a Subarray
Given an array of integers, determine whether it is possible to sort the array in ascending order by reversing exactly one contiguous subarray. More formally, find indices \(l\) and \(r\) (with \(1 \le l \le r \le n\)) such that by reversing the subarray between these two indices, the entire array becomes sorted in non-decreasing order. Note that if the array is already sorted, the answer is considered "YES".
Input/Output Example:
Input: 5 1 3 2 4 5</p>Output: YES
In the above example, reversing the subarray from the second element to the third element (i.e. [3,2] becomes [2,3]) results in the sorted array [1,2,3,4,5].
inputFormat
The first line contains an integer \(n\), the number of elements in the array. The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Print a single line containing "YES" if the array can be sorted into ascending order by reversing exactly one contiguous subarray, and "NO" otherwise.
## sample5
1 3 2 4 5
YES
</p>