# 2972. Count the Number of Incremovable Subarrays II

## Problem Statement

<br>

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.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [1,2,3,4]
</strong><strong>Output: 10
</strong><strong>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.
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [6,5,7,8]
</strong><strong>Output: 7
</strong><strong>Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
</strong>It can be shown that there are only 7 incremovable subarrays in nums.
</code></pre>

**Example 3:**

<pre><code><strong>Input: nums = [8,7,6,6]
</strong><strong>Output: 3
</strong><strong>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.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`

## Intuition

```
Approach:

Find the left index till what incresing and 
right till what increasing 

Then try to find subarrays count, by taking array from left and picking 
Corresponding right array

Use upperbound to find the right array increasing 
```

### Links

<https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii/>

### Video Links

<https://youtu.be/VmTxOpyPtIE?t=4315>

### Approach 1:

```
NlongN
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    long long incremovableSubarrayCount(vector<int>& nums) {
        int n = nums.size();
        int left = 0, right = n-1;
        
        while(left+1 < n and nums[left] < nums[left+1])
            left++;

        while(right-1 >= 0 and nums[right] > nums[right-1])
            right--;

        if(left == n-1)
            return (n*(n+1))/2;
        
        long long ans = n-right+1;
        for(int i=0; i<=left; i++){
            int upidx = upper_bound(nums.begin()+right, nums.end(), nums[i])-nums.begin();
            ans += n-upidx+1;
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/binary-search/bs-on-1d-array/2972.-count-the-number-of-incremovable-subarrays-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
