Problem Description
Given a 0-indexed array of positive integers, count the number of non‐empty contiguous subarrays (called “incremovable”) that, when removed from the array, leave the remaining elements (in order) forming a strictly increasing sequence. (Note that an empty array is considered strictly increasing.)
For example, in the array [5,3,4,6,7] removing the subarray [3,4] yields [5,6,7] which is strictly increasing.
Key Insights
- Removing a subarray divides the array into a left part (before removal) and a right part (after removal). Both parts, taken alone, must be strictly increasing.
- When both parts are non-empty, an extra “bridge condition” is required: the last element of the left part must be less than the first element of the right part.
- Precomputing whether each prefix (from index 0 up to i) and each suffix (from index i to end) is strictly increasing helps quickly decide if the remaining segments are good.
- The challenge is to count in O(n log n) or O(n) time without explicitly iterating over every O(n²) subarray.
- One strategy is to treat removals in three cases: removals that remove a prefix (no left part), removals that remove a suffix (no right part) and removals “in the middle” (both parts exist). For the middle removals, binary search over candidate indices (using precomputed “suffix valid” indices) can help enforce the bridge condition.
Space and Time Complexity
Time Complexity: O(n log n) – n for scanning the array plus up to O(n log n) from binary searches. Space Complexity: O(n) – to store prefix and suffix validity arrays (and an auxiliary list for binary search).
Solution
The key idea is to note that if you remove the subarray nums[i…j], the remaining array becomes the concatenation of left = nums[0…i-1] and right = nums[j+1…n-1]. For the result to be strictly increasing: • If the left part exists (i > 0), then nums[0 … i-1] must be strictly increasing. • If the right part exists (j < n-1), then nums[j+1 … n-1] must be strictly increasing. • And if both parts exist, nums[i-1] must be strictly less than nums[j+1].
To efficiently count valid removals:
- Precompute two boolean arrays: one for prefix validity (is the subarray nums[0 … i] strictly increasing?) and one for suffix validity (is the subarray nums[i … n-1] strictly increasing?).
- Count removals in three separate cases:
- Case A: Removing a prefix (i == 0). In this case no left part exists so we only require that the right part (if non-empty) is strictly increasing.
- Case B: Removing a suffix (j == n-1). Here no right part exists so only the left part needs to be strictly increasing.
- Case C: Removing a subarray in the middle (i > 0 and j < n-1). In addition to the left and right parts being individually strictly increasing, the boundary condition nums[i-1] < nums[j+1] must hold.
- For Case C, we iterate over valid starting indices (i from 1 to n-1 for which prefix ending at i–1 is strictly increasing) and use binary search (on a precollected list of indices from the right part with valid suffix property) to quickly count the number of j values (with j >= i and j < n-1) that satisfy both the right part validity and the bridge condition.
Finally, add the counts from all three cases. Special attention is needed to avoid double counting removals that are both prefix and suffix removals (i.e. removing the entire array).