Problem Description
Given an integer array, the task is to find the shortest continuous subarray such that when only that subarray is sorted (in non-decreasing order), the entire array becomes sorted in non-decreasing order. If the array is already sorted, the answer is 0.
Key Insights
- The sorted order of the array determines the positions where the actual array deviates from the sorted order.
- A straightforward solution is to compare the array with a sorted copy and identify the first and last indices where they differ.
- A more optimal O(n) approach involves traversing the array from left-to-right and right-to-left:
- Left-to-right pass: Track the maximum value seen. Whenever the current number is less than this maximum, it indicates the elements are not in order; update the right boundary accordingly.
- Right-to-left pass: Track the minimum value seen. Whenever the current number is greater than this minimum, update the left boundary.
- If the array is already sorted, the boundaries never update, and the result is 0.
- This approach uses constant extra space (besides a few variables), making it efficient for large inputs.
Space and Time Complexity
Time Complexity: O(n) – The array is traversed twice. Space Complexity: O(1) – Only constant extra space is used.
Solution
The solution uses a two-pointer greedy approach:
- In the first pass (left-to-right), track the maximum value encountered so far. When a current element is found smaller than this maximum, mark its position as a potential right boundary.
- In the second pass (right-to-left), track the minimum value encountered so far. When a current element is larger than this minimum, mark its position as a potential left boundary.
- The difference between these boundary indices (plus one) yields the length of the shortest unsorted subarray.
- If no such boundaries are found, then the array is already sorted, and the answer is 0.
This method avoids extra space for a sorted copy and achieves optimal time performance.