#K2351. Sorting Array by One Reverse Operation

    ID: 24717 Type: Default 1000ms 256MiB

Sorting Array by One Reverse Operation

Sorting Array by One Reverse Operation

Given an array of n integers, determine if the array can be sorted in ascending order by performing exactly one reverse operation on a contiguous subarray. More formally, find an index (i) (0-based) such that if we reverse the subarray from index (i) to the end, the entire array becomes sorted. In other words, let (S) be the sorted version of the array. The task is to check if there exists an index (i) for which [ A[i:] = \text{reverse}(S[i:]) ] If such an index exists, output "YES"; otherwise, output "NO". This operation must be applied exactly once per test case.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (T) representing the number of test cases. Each test case consists of a line starting with an integer (n) (the number of elements) followed by (n) space-separated integers.

outputFormat

For each test case, output a single line. Print "YES" if the array can be sorted by performing exactly one reverse operation (as described), otherwise print "NO".## sample

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

YES YES

</p>