


Let’s say we have an array 'ARR' {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60}. Then we have to find the subarray {30 , 25 , 40 , 32 , 31 , 35} and print its length = 5 as our output. Because, when we sort this subarray the whole array will be sorted.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains one integer ‘N’ denoting the number of elements present in the array.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
For each test case, print the shortest length of the unsorted subarray. Output for each test case is printed on a separate line.
You do not need to print anything, it has already been taken care of. Just return the length of the shortest subarray.
1 <= T <= 10
1 <= N <= 5 * 10 ^ 4
-10^5 <= ARR[i] <= 10^5
Where ‘T’ represents the number of test cases, ‘N’ represents the number of elements present in the array, and ‘ARR[i]’ represents the array element.
Time Limit: 1sec
The algorithm is as follows:
This approach is based on the idea of selection sort. But instead of swapping as we do in the case of selection sort, we will determine the leftmost boundary of the unsorted array and the rightmost boundary of the unsorted subarray.
This approach is based on the idea of sorting. Here we will copy the given array in another array and then sort it and then compare this array with the given array. We will determine the leftmost and the rightmost position, where the element at this position mismatches. And the subarray lying between these two positions is the unsorted subarray.
This approach uses two passes of the array. In the first pass, Iterate from the end, and check whether the array is in descending order, i.e., ARR[i] > ARR[i-1] > ARR[i-2] > … . If not we will find the minimum index at which the condition breaks. In the second pass, Iterate from the start, and check whether the array is in ascending order, i.e., ARR[i] < ARR[i+1] < ARR[i+2] < …. If not we find the maximum index at which the condition breaks.
Let's say we have an array ARR = {2, 6, 4, 8,10,9,15} (consider 0 based indexing).
Here n = 7 , start = -1 , end = n
Also curMin = inf curMax = -inf
Now first traverse the array from right to left we have two conditions:
First: if(curMin > Arr[i]) then curMin = Arr[i]
Second: if(Arr[i] > curMin) then update start = i
For i = 6 (Arr[6] = 15) :
curMin = 15
start = -1
For i = 5 (Arr[5] = 9) :
curMin = 9
start = -1
For i = 4 (Arr[4] = 10) :
curMin = 15
start = 4
For i = 3 (Arr[3] = 8) :
curMin = 8
start = 4
For i = 2 (Arr[2] = 4) :
curMin = 4
start = 4
For i = 1 (Arr[1] = 6) :
curMin = 4
start = 1
For i = 0 (Arr[0] = 2) :
curMin = 2
start = 1
Now traverse the array from left to right we have two conditions:
First: if(curMax < Arr[i]) then curMax = Arr[i]
Second: if(Arr[i] < curMax) then end = i
For i = 0 (Arr[0] = 2) :
curMax = 2
end = 7
For i = 1 (Arr[1] = 6) :
curMax = 6
end = 7
For i = 2 (Arr[2] = 4) :
curMax = 6
end = 2
For i = 3 (Arr[3] = 8) :
curMax = 8
end = 2
For i = 4 (Arr[4] = 10) :
curMax = 10
end = 2
For i = 5 (Arr[5] = 9) :
curMax = 10
end = 5
For i = 6 (Arr[6] = 15) :
curMax = 15
end = 5
At the end, we have ‘start’ = 1 and ‘end’ = 5
Hence subarray will be { 6, 4, 8,10,9}