#K46577. Reverse Subarray Sorting

    ID: 28007 Type: Default 1000ms 256MiB

Reverse Subarray Sorting

Reverse Subarray Sorting

You are given a bookshelf represented as an array of integers, where each integer denotes the height of a book. Your task is to determine if the bookshelf can be sorted in non-decreasing order by reversing exactly one contiguous subarray.

Formally, given an array \(a_1, a_2, \dots, a_n\), determine whether there exist indices \(l\) and \(r\) (with \(1 \le l \le r \le n\)) such that reversing the subarray \(a_l, a_{l+1}, \dots, a_r\) results in the entire array being sorted in non-decreasing order. If the bookshelf is already sorted, the answer should be "YES".

inputFormat

The first line contains an integer (T) denoting the number of test cases. For each test case, the first line contains an integer (n) representing the number of books. The next line contains (n) space-separated integers denoting the heights of the books. Note that (n) can be 0, indicating an empty shelf.

outputFormat

For each test case, output a line containing either "YES" if the bookshelf can be sorted by reversing exactly one contiguous subarray, or "NO" otherwise.## sample

5
5
1 2 3 4 5
3
3 2 1
5
2 1 3 4 5
5
1 5 3 4 2
1
1
YES

YES YES NO YES

</p>