Count the Number of Incremovable Subarrays I - Problem

You are given a 0-indexed array of positive integers nums. A subarray of nums is called incremovable if nums becomes strictly increasing after removing the subarray.

For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.

Return the total number of incremovable subarrays of nums.

Note that an empty array is considered strictly increasing. A subarray is a contiguous non-empty sequence of elements within an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [5,3,4,6,7]
Output: 6
💡 Note: The 6 incremovable subarrays are: [3], [4], [3,4], [3,4,6], [3,4,6,7], [6,7]. After removing any of these, the remaining array is strictly increasing.
Example 2 — Already Sorted
$ Input: nums = [1,2,3,4]
Output: 10
💡 Note: Since the array is already strictly increasing, we can remove any subarray. Total subarrays = n(n+1)/2 = 4×5/2 = 10.
Example 3 — Minimum Size
$ Input: nums = [6,5]
Output: 3
💡 Note: We can remove [6], [5], or [6,5]. All result in strictly increasing (or empty) arrays.

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50

Visualization

Tap to expand
Count Incremovable Subarrays: [5,3,4,6,7] → 6 valid removals53467Original Array✓ Remove [3] → [5,4,6,7]? No✓ Remove [4] → [5,3,6,7]? No✓ Remove [3,4] → [5,6,7]? Yes!✓ Remove [3,4,6] → [5,7]? Yes!✓ Remove [3,4,6,7] → [5]? Yes!✓ Remove [6,7] → [5,3,4]? NoTotal: 6 incremovable subarrays found
Understanding the Visualization
1
Input Array
Given array [5,3,4,6,7] with mixed order
2
Try Removals
Test removing different contiguous subarrays
3
Count Valid
Count subarrays whose removal makes array strictly increasing
Key Takeaway
🎯 Key Insight: An incremovable subarray creates a strictly increasing remainder when the left and right parts connect properly
Asked in
Microsoft 8 Google 5
12.5K Views
Medium Frequency
~25 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen