Linear Time complexity , well explained 
Intuition 1For 1Number of possibilities are1
Total cases: 1
adding 1 more number 1,212
2Total cases: previous + size of current sub array -> 1 + 2 -> 3adding 1
# Intuition 1

For **1**

Number of possibilities are 

1
**Total cases: 1**
adding 1 more number **1,2**

12
 2

**Total cases: previous + size of current sub array -> 1 + 2 -> 3**
 
adding 1 more number **1,2,3**

123
 23
  3

**Total cases: previous + size of current sub array -> 3 + 3 -> 6**

adding 1 more number **1,2,3,4**

1234
 234
  34
   4


**Total cases: previous + size of current sub array -> 6 + 4 -> 10**

*So for each new number added we have to add size to previous possibilities.*

Now comes the intresting part. 

We have to consider the constraint that the max number in the sub arry should be in range [left,right]

# Intuition 2

So let consider this array 

1,2,3,1,1,1,2

left = 2;
right = 3;

**Iteration 1:**

sub array : 1

possiblities will be 

1

but 1 is less that left so dont add to sum.

**Iteration 2:**

sub array 1,2
previous sum = 0

as we have seen in intuition 
Total cases: previous + size of current sub array -> 0 + 2 -> 2

**Iteration 3:**

sub array 1,2,3
previous sum = 2

Total cases: previous + size of current sub array -> 2 + 3 -> 5

**Iteration 4:**

sub array 1,2,3,1
previous sum = 5

Let dive in new cases added

1231
 231
  31
   1

**Wait here we should not consider the last case**

Total cases: previous + size of current sub array - excluded case -> 5 + 4 -1 -> 8

**Iteration 5:**

sub array 1,2,3,1,1
previous sum = 8

Let dive in new cases added

12311
 2311
  311
   11
    1

Wait here we should not consider the last two cases case 

Total cases: previous + size of current sub array - excluded case -> 8 + 5 - 2 -> 11

Hmm we have to subtract  previously excluded cases + 1

**Iteration 6:**

sub array 1,2,3,1,1,1
previous sum = 11

Let dive in new cases added

123111
 23111
  3111
   111
    11
     1

**Wait here we should not consider the last two cases case**

Total cases: previous + size of current sub array - excluded case ->  11 + 6 - 3 -> 14

**Final iteration**

sub array 1,2,3,1,1,1
previous sum = 14

Let dive in new cases added

1231112
 231112
  31112
   1112
    112
     12
      2

Oh here we dont have to exclude any cases when nums[i] > left

Total cases: previous + size of current sub array - excluded case ->  14 + 7 -> 21


# Intuition 3

Consider 2,3,5,2

**Iteration 1**

Sub array 2

Ans: 1

**Iteration 2**

Sub array 2,3
Ans: 1 + 2 = 3

**Iteration 3**

Sub array 2,3,5

Now element is 5 > right.

So 5 cannot be included in any aaray to move on 

Iteration 

New Sub array 2

Ans: 3 + 1 = 4

So here is the conclusion 
 
1) For each new element added, no of possibilities increase by sub array size
2) For element less that left we need to keep track of them 
3) For Element greater that right we need to start a new sub array

# Code
```java []
class Solution {
    public int numSubarrayBoundedMax(int[] nums, int left, int right) {
        
        int len = nums.length;
        if(len == 0) {
            return 0;
        }

        int ans = 0;
        int n = 0;
        int i = 0 ;
        int s = 0 ;

        while(i<len) {
            if(nums[i] > right) {
                n=0;
                s=0;
            } else if(nums[i] < left) {
                s++;
                n++;
            } else {
                s = 0;
                n++;
            }
            ans = ans + n - s;
            i++;
        }
        return ans;
    }
}
```

# Complexity
- Time complexity:
O(n)


['Array', 'Java']