#C1086. Sort Array by Reversing a Subarray

    ID: 40111 Type: Default 1000ms 256MiB

Sort Array by Reversing a Subarray

Sort Array by Reversing a Subarray

You are given an integer \( n \) and an array \( a \) 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 subarray. Formally, you need to check if there exist indices \( l \) and \( r \) (with \( 1 \le l \le r \le n \)) such that if you reverse the subarray \( a[l, r] \), the resulting array becomes sorted in non-decreasing order.

Note that if the array is already sorted, it is considered valid and you should output "YES".

Example:

Input:  4
        1 3 2 4
Output: YES

Here, reversing the subarray [3, 2] gives [1, 2, 3, 4], which is sorted.

inputFormat

The first line of input contains an integer \( T \) denoting the number of test cases. For each test case:

  • The first line contains an integer \( n \) — the number of elements in the array.
  • The second line contains \( n \) space-separated integers representing the array \( a \).

All input is read from standard input.

outputFormat

For each test case, print a single line containing "YES" if it is possible to sort the array by reversing exactly one contiguous subarray (or if the array is already sorted), otherwise print "NO". The outputs for all test cases should be printed to standard output.

## sample
3
4
1 3 2 4
4
1 2 3 5
5
3 2 1 5 4
YES

YES NO

</p>