#K60882. Subarray Reversal Sorting

    ID: 31185 Type: Default 1000ms 256MiB

Subarray Reversal Sorting

Subarray Reversal Sorting

You are given an array of integers. Your task is to determine whether it is possible to sort the array in non-decreasing order by applying at most one subarray reversal operation, where the allowed subarray to reverse is exactly of length 3.

In other words, for each test case, you are allowed to pick a contiguous subarray of length 3 and reverse it once (or not at all) in order to try to transform the original array into a sorted (non-decreasing) array. If the array is already sorted, no reversal is needed.

The challenge is to decide if there exists a subarray of length 3 such that reversing it will make that particular segment match the corresponding segment of the sorted array. Note that if the array is already sorted, the answer is automatically "YES".

Input Constraints:

  • 1 ≤ T ≤ 104, where T is the number of test cases.
  • 1 ≤ n ≤ 105 for each test case.
  • The array elements are integers that can be repeated.

Note: Reversal is only allowed on subarrays of exactly three consecutive elements.

inputFormat

The first line contains an integer T, the number of test cases.

Then for each test case, the first line contains an integer n, the size of the array. The second line contains n space-separated integers representing the array.

outputFormat

For each test case, output a single line containing either "YES" if the array can be sorted (after at most one reversal of a subarray of three consecutive elements) or "NO" otherwise.

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

NO YES YES

</p>