Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on 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.
Example 1:
Input: nums = [1,2,3,4] Output: 10 Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8] Output: 7 Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6] Output: 3 Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50Problem summary: You are given a 0-indexed array of positive integers nums. A subarray of nums is called incremovable if nums becomes strictly increasing on 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search
[1,2,3,4]
[6,5,7,8]
[8,7,6,6]
shortest-subarray-to-be-removed-to-make-array-sorted)number-of-subarrays-that-match-a-pattern-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
class Solution {
public int incremovableSubarrayCount(int[] nums) {
int i = 0, n = nums.length;
while (i + 1 < n && nums[i] < nums[i + 1]) {
++i;
}
if (i == n - 1) {
return n * (n + 1) / 2;
}
int ans = i + 2;
for (int j = n - 1; j > 0; --j) {
while (i >= 0 && nums[i] >= nums[j]) {
--i;
}
ans += i + 2;
if (nums[j - 1] >= nums[j]) {
break;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
func incremovableSubarrayCount(nums []int) int {
i, n := 0, len(nums)
for i+1 < n && nums[i] < nums[i+1] {
i++
}
if i == n-1 {
return n * (n + 1) / 2
}
ans := i + 2
for j := n - 1; j > 0; j-- {
for i >= 0 && nums[i] >= nums[j] {
i--
}
ans += i + 2
if nums[j-1] >= nums[j] {
break
}
}
return ans
}
# Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
i, n = 0, len(nums)
while i + 1 < n and nums[i] < nums[i + 1]:
i += 1
if i == n - 1:
return n * (n + 1) // 2
ans = i + 2
j = n - 1
while j:
while i >= 0 and nums[i] >= nums[j]:
i -= 1
ans += i + 2
if nums[j - 1] >= nums[j]:
break
j -= 1
return ans
// Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
// class Solution {
// public int incremovableSubarrayCount(int[] nums) {
// int i = 0, n = nums.length;
// while (i + 1 < n && nums[i] < nums[i + 1]) {
// ++i;
// }
// if (i == n - 1) {
// return n * (n + 1) / 2;
// }
// int ans = i + 2;
// for (int j = n - 1; j > 0; --j) {
// while (i >= 0 && nums[i] >= nums[j]) {
// --i;
// }
// ans += i + 2;
// if (nums[j - 1] >= nums[j]) {
// break;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2970: Count the Number of Incremovable Subarrays I
function incremovableSubarrayCount(nums: number[]): number {
const n = nums.length;
let i = 0;
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
if (i === n - 1) {
return (n * (n + 1)) / 2;
}
let ans = i + 2;
for (let j = n - 1; j; --j) {
while (i >= 0 && nums[i] >= nums[j]) {
--i;
}
ans += i + 2;
if (nums[j - 1] >= nums[j]) {
break;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.