#K69562. Sort Array by Reversing a Subarray

    ID: 33114 Type: Default 1000ms 256MiB

Sort Array by Reversing a Subarray

Sort Array by Reversing a Subarray

You are given an array of n integers. Your task is to determine whether the array can be made non-decreasing by reversing at most one continuous subarray. In other words, you need to check if there exists a subarray that, when reversed, results in the entire array being sorted in non-decreasing order.

If it is possible, output YES. Otherwise, output NO.

The problem can be formally described as follows:

Given an array \( A = [a_1, a_2, \dots, a_n] \), determine if there exists indices \( i \) and \( j \) with \(1 \leq i < j \leq n\) such that if you reverse the subarray \( A[i\dots j] \), the resulting array will be sorted in non-decreasing order. If the array is already sorted, then the answer is YES.

Note: A subarray is defined as a contiguous segment of the array.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains a single integer \( n \) representing the number of elements in the array.
  2. The second line contains \( n \) space-separated integers representing the array \( A \).

outputFormat

Output a single line to standard output (stdout) that is either YES or NO depending on whether the array can be sorted by reversing at most one subarray.

## sample
6
3 6 5 4 7 9
YES

</p>