#K81767. Swap to Beautiful Array
Swap to Beautiful Array
Swap to Beautiful Array
An array is considered beautiful if it is sorted in non-decreasing order, i.e. \(a_1 \le a_2 \le \cdots \le a_n\). You are given an array of integers and you are allowed to perform exactly one swap between any two distinct elements. Note that if the array is already beautiful, it is considered valid. Your task is to determine whether it is possible to obtain a beautiful array by performing at most one swap operation.
For example, consider the array [2, 4, 3, 5]. By swapping the elements 4 and 3, we obtain [2, 3, 4, 5], which is beautiful. Conversely, the array [5, 4, 3, 2] cannot be made beautiful with just one swap.
inputFormat
The input is given from stdin
and consists of multiple test cases. The first line contains an integer T (\(1 \le T \le 100\)), the number of test cases. Each test case begins with an integer n (the number of elements in the array), followed by a line containing n space-separated integers.
Input format:
T n a1 a2 ... an ...
outputFormat
For each test case, output a single line to stdout
containing either YES
if it is possible to make the array beautiful by performing at most one swap, or NO
otherwise.
3
4
2 3 4 5
4
5 4 3 2
4
2 4 3 5
YES
NO
YES
</p>