Watch the video solution for Smallest Subarray to Sort in Every Sliding Window, a medium level problem involving Array, Two Pointers, Stack. This walkthrough by Owen Wu has 149 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums and an integer k.
For each contiguous subarray of length k, determine the minimum length of a continuous segment that must be sorted so that the entire window becomes non‑decreasing; if the window is already sorted, its required length is zero.
Return an array of length n − k + 1 where each element corresponds to the answer for its window.
Example 1:
Input: nums = [1,3,2,4,5], k = 3
Output: [2,2,0]
Explanation:
nums[0...2] = [1, 3, 2]. Sort [3, 2] to get [1, 2, 3], the answer is 2.nums[1...3] = [3, 2, 4]. Sort [3, 2] to get [2, 3, 4], the answer is 2.nums[2...4] = [2, 4, 5] is already sorted, so the answer is 0.Example 2:
Input: nums = [5,4,3,2,1], k = 4
Output: [4,4]
Explanation:
nums[0...3] = [5, 4, 3, 2]. The whole subarray must be sorted, so the answer is 4.nums[1...4] = [4, 3, 2, 1]. The whole subarray must be sorted, so the answer is 4.
Constraints:
1 <= nums.length <= 10001 <= k <= nums.length1 <= nums[i] <= 106Problem Overview: You are given an array and a sliding window. For each window, determine the smallest subarray that must be sorted so the entire window becomes sorted. The challenge is detecting the minimal unsorted region efficiently without repeatedly sorting every window.
Approach 1: Brute Force Window Check (Sorting Each Window) (Time: O(n * k log k), Space: O(k))
Iterate through every sliding window of size k. For each window, copy the elements and sort them using a standard sorting algorithm. Compare the sorted version with the original window to find the first and last indices where values differ. That range represents the smallest subarray that must be sorted. This approach is straightforward and useful for understanding the problem constraints, but repeatedly sorting each window is expensive and becomes slow for large arrays.
Approach 2: Monotonic Stack Boundary Detection (Time: O(n), Space: O(n))
A common technique for identifying unsorted regions uses a monotonic stack. Scan from left to right and maintain a stack of increasing elements. When a smaller element appears, it breaks the sorted order and indicates that earlier elements belong in the unsorted region. Repeat from the right side with a decreasing stack to determine the right boundary. The stack helps detect disorder efficiently without sorting. While powerful, managing stack state across sliding windows can add implementation complexity.
Approach 3: Enumeration with Left Maximum and Right Minimum (Time: O(n), Space: O(n))
This approach relies on tracking violations of sorted order using prefix and suffix information. While iterating through the array, maintain the maximum value seen from the left. If the current element is smaller than this left maximum, it must belong to the unsorted region. Similarly, track the minimum value from the right to detect elements that are larger than a future value. These two passes identify the minimal boundaries that need sorting. The method works because any element smaller than a previous maximum or larger than a later minimum breaks sorted order. The logic uses simple array scans and fits naturally with array processing patterns.
Recommended for interviews: Interviewers typically expect the linear scan solution using maintained boundaries (left maximum and right minimum). Starting with the brute force explanation shows you understand the problem definition, but the O(n) boundary detection demonstrates algorithmic maturity and efficient reasoning about sorted order violations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Window Sorting | O(n * k log k) | O(k) | Small constraints or when validating logic quickly |
| Monotonic Stack Boundary Detection | O(n) | O(n) | When detecting disorder boundaries using stack patterns |
| Left Max / Right Min Enumeration | O(n) | O(n) | Optimal general solution for large arrays and interview settings |