#K59027. Single Swap for Consecutive Sequence

    ID: 30773 Type: Default 1000ms 256MiB

Single Swap for Consecutive Sequence

Single Swap for Consecutive Sequence

You are given an array of n integers. Your task is to determine whether it is possible to rearrange the array into a sequence of consecutive numbers (i.e., an arithmetic progression with a common difference of 1) by performing at most one swap between any two elements.

If the array, when sorted, forms a consecutive sequence, then the answer is YES. Otherwise, if a single swap between any two elements can result in a consecutive sequence, output YES; if not, output NO.

For example:

  • For the array [1, 3, 2], a single swap can rearrange it to [1, 2, 3], which is consecutive, so the output is YES.
  • For the array [2, 4, 3], a single swap can yield [2, 3, 4], hence the output is YES.
  • For the array [1, 4, 3], no single swap can result in a fully consecutive sequence, so the output is NO.

inputFormat

The input is given on standard input (stdin) and consists of multiple test cases. The first line contains an integer T representing the number of test cases. For each test case, the first line contains an integer n (the size of the array) and the second line contains n space-separated integers representing the array elements.

outputFormat

For each test case, print a single line on standard output (stdout) containing YES if the array can be rearranged into a consecutive sequence by performing at most one swap, or NO otherwise.

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

YES NO

</p>