
The first line contains ‘T’, denoting the number of tests.
For each Test :
The first line contains an integer ‘N’, denoting the length of the array.
The second line contains an array ‘A’ of length ‘N’.
For each test, print an integer, denoting the maximum number of segments in which you can break the array and still get the final array sorted.
1<= ‘T’ <= 5
1<= ‘N’ <= 10^5
0<= A[i] <= 10^9 i ∈ (1,N)
Time Limit: 1 sec
Algorithm:
If the maximum of all the values to its left is less or equal to the minimum value to its right, then we can end a segment at the ‘ith’ index. I.e max(a1, a2, .. , ai) <= min(a(i + 1), … , an). The condition is true because if the minimum value on the right (Let’s say ‘ar’) to index ‘i’ is smaller than the largest value from the left (let’s say ‘al’). Suppose we end the segment at the index ‘i’. We can’t bring the smaller value ‘ar’ before, the larger value ‘al’. The final array might look like [..., al], [ar, ....] since ‘al’ > ‘ar’. Hence the array will not be sorted.
Algorithm: