#K41502. Can Sort Array by Reversing a Subarray
Can Sort Array by Reversing a Subarray
Can Sort Array by Reversing a Subarray
Given an array of n integers, determine if it can be sorted into non-decreasing order by reversing exactly one contiguous subarray. Formally, for an array \( A = [a_1, a_2, \dots, a_n] \), check if there exist indices \( l \) and \( r \) (with \( 1 \le l < r \le n \)) such that reversing the subarray \( [a_l, a_{l+1}, \dots, a_r] \) results in a sorted array. If the array is already sorted, it is considered valid. Print YES
if it is possible, otherwise print NO
.
Note: The reversal operation is applied only once.
inputFormat
The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers denoting the elements of the array.
outputFormat
Output a single line: YES
if the array can be sorted by reversing one contiguous subarray; otherwise, output NO
.## sample
5
1 3 2 4 5
YES