Maximum Unsorted Subarray
Last updated
Last updated
Given an array, A of non-negative integers of size N. Find the minimum sub-array Al, Al+1 ,..., Ar such that if we sort(in ascending order) that sub-array, then the whole array should get sorted. If A is already sorted, output -1. Problem Constraints
1 <= N <= 1000000 1 <= A[i] <= 1000000 Input Format
The first and only argument is an array of non-negative integers of size N. Output Format
Return an array of length two where the first element denotes the starting index(0-based) and the second element denotes the ending index(0-based) of the sub-array. If the array is already sorted, return an array containing only one element i.e. -1. Example Input
Input 1:
Input 2:
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
Explanation 2:
Apparently, maxRight
and minLeft
is useless.
Whatever matches between maxLeft
and minRight
is what is properly sorted.
What doesn't match is what needs to be sorted.
If the array were already soretd:
This tells us that if the array is already sored, then everything in the maxLeft
and minRight
will match.