You are given a permutation 'A' of length 'N'. You can apply the following operation any number of times which costs 1 coin for each operation.
Select any subarray of length at most 'N' - 1 and rearrange the elements in any order.
Return the minimum number of coins required to sort the permutation in increasing order.
A permutation is an array in which each element from 1 to 'N' occurs exactly once.
For Example:-Let 'N' = 5, 'A' = [1, 2, 3, 5, 4].
We can apply the operation to the subarray from index 4 to 5 (1-based indexing).
So our answer is 1.
First-line contains an integer 'T', which denotes the number of test cases.
For every test case:-
First-line contains an integer 'N', denoting the length of the array 'A'.
Second-line contains 'N' space-separated integers, elements of array 'A'.
Output Format:-
For each test case, Return the minimum number of coins required to sort the permutation in increasing order.
Note:-
You don’t need to print anything. Just implement the given function.
1 <= 'T' <= 10
3 <= 'N' <= 10^5
The Sum of 'N' overall test cases does not exceed 10^5.
Time Limit: 1 sec
2
4
1 3 2 4
3
1 2 3
1
0
First test case:-
We can apply the operation to the subarray from index 2 to 3 (1-based indexing).
So our answer is 1.
Second test case:-
The array is already sorted.
So our answer is 0.
2
5
5 4 3 1 2
5
3 2 1 4 5
2
1
Can the answer be greater than 3?
Approach:-
Algorithm:-
O(N), where 'N' is the length of 'A'.
We are traversing in 'A' to check whether 'A' is sorted or not and the complexity associated with this is O(N). Hence, the overall time complexity is of the order O(N).
O(1).
We are using constant extra space, So the Space Complexity is O(1).