Pseudo Sorted Array Solution | CodeChef Problem Solution 2022 | April Long Two
An array of length is said to be pseudo-sorted if it can be made non-decreasing after performing the following operation at most once.
- Choose an such that and swap and
Given an array , determine if it is pseudo-sorted or not.
Input Format
- The first line contains a single integer - the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer - the size of the array .
- The second line of each test case contains space-separated integers denoting the array .
Output Format
For each testcase, output YES
if the array is pseudo-sorted, NO
otherwise.
You may print each character of YES
and NO
in uppercase or lowercase (for example, yes
, yEs
, Yes
will be considered identical).
Constraints
- Sum of over all test cases do not exceed
Sample Input 1
3
5
3 5 7 8 9
4
1 3 2 3
3
3 2 1
Sample Output 1
YES
YES
NO
Explanation
Test case 1: The array is already sorted in non-decreasing order.
Test case 2: We can choose and swap and . The resulting array will be , which is sorted in non-decreasing order.
Test case 3: It can be proven that the array cannot be sorted in non-decreasing order in at most one operation.
No comments:
Post a Comment