Concat Sort solution codechef – JJ has an array A. He can perform the following operation on A:
- Divide A into two subsequences P and Q such that each A_i belongs to either P or Q.
- Set A := P\ \texttt{concat}\ Q
Table of Contents
[Solution] Concat Sort solution codechef
Here \texttt{concat} denotes the concatenation operation. For e.g. [2, 1, 5] \texttt{ concat } [4, 3] = [2, 1, 5, 4, 3].
Is it possible to make A sorted in non-decreasing order after applying the above operation at most once?
Note: An array X is a subsequence of an array Y if X can be obtained by deletion of several (possibly, zero or all) elements from Y.
Input Format
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_1, A_2, \ldots, A_N denoting the array A.
Output Format
For each test case, output YES
if it is possible to make the array A sorted after applying the given operation at most once. Otherwise, output NO
.
You may print each character of YES
and NO
in uppercase or lowercase (for example, yes
, yEs
, Yes
will be considered identical).
[Solution] Concat Sort solution codechef
- 1 \leq T \leq 10^5
- 2 \leq N \leq 10^5
- 1 \le A_i \le 10^9
- Sum of N over all test cases does not exceed 2 \cdot 10^5.
Sample 1:
3 6 4 5 6 1 2 3 5 1 3 5 2 4 5 5 10 7 11 9
YES NO YES
[Solution] Concat Sort solution codechef Explanation:
Test case 1: We can select P = [1, 2, 3] and Q = [4, 5, 6]. Therefore A will become [1, 2, 3, 4, 5, 6] which is sorted.
Test case 2: It can be proven that it is not possible to sort A by using the given operation at most once.
Test case 3: We can select P = [5, 7, 9] and Q = [10, 11]. Therefore A will become [5, 7, 9, 10, 11] which is sorted.