You are given an integer array ‘ARR’. You have to find the length of the shortest contiguous subarray such that, if you sort this subarray in ascending order, then the whole array will be sorted in ascending order.
An array 'C' is a subarray of array 'D' if it can be obtained by deletion of several elements(possibly zero) from the beginning and the end from array 'D'.
Example:
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.
Input format:
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.
Output format:
For each test case, print the shortest length of the unsorted subarray. Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just return the length of the shortest subarray.
Constraints:
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 question asks to find the length of the shortest contiguous subarray that needs to be sorted in order to sort the whole array.
Iterate through the array and find the first element that is out of or...read more
In this approach, we consider every possible subarray that can be formed from the given array ‘ARR’. For every subarray ARR[i … j] considered, we need to check whether this is the smallest ...read more
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 th...read more
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 leftmo...read more
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...read more
Top ZeMoSo Technologies Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Reviews
Interviews
Salaries
Users/Month