#K49962. Minimum Operations to Sort a Permutation
Minimum Operations to Sort a Permutation
Minimum Operations to Sort a Permutation
You are given a permutation of numbers from 1 to n. In one operation, you remove the first element of the permutation. Your task is to determine the minimum number of operations required such that the remaining array is sorted in strictly increasing order.
In other words, find the smallest non-negative integer k such that after removing the first k elements from the permutation, the resulting sequence is sorted in ascending order. If the permutation is already sorted, no operation is needed and the answer is 0.
Example Explanation:
- For the permutation [3, 4, 2, 1]: Removing the first 3 elements leaves [1], which is sorted, so the answer is 3.
- For the permutation [1, 2, 3, 4, 5, 6]: The permutation is already sorted so the answer is 0.
inputFormat
The input is given via standard input (stdin) and has the following format:
T n p1 p2 ... pn n p1 p2 ... pn ... (T test cases in total)
Where:
- T is the number of test cases.
- For each test case, the first line contains an integer n, the length of the permutation.
- The second line contains n space-separated integers representing the permutation.
outputFormat
For each test case, output a single line containing the minimum number of operations required to make the remaining array sorted in ascending order. The output should be written to standard output (stdout).
## sample3
4
3 4 2 1
5
5 2 3 4 1
6
1 2 3 4 5 6
3
4
0
</p>