Given an integer array nums and two integers left and right, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right].

The test cases are generated so that the answer will fit in a 32-bit integer.

&nbsp;
Example 1:

Input: nums = [2,1,4,3], left = 2, right = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].


Example 2:

Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	0 &lt;= nums[i] &lt;= 109
	0 &lt;= left &lt;= right &lt;= 109


class Solution:
    def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
        n = len(nums)
        stack = []
        next_greater = [n] * n
        prev_greater = [-1] * n
        for i in range(n):
            while len(stack) > 0 and nums[i] > nums[stack[-1]]:
                curr = stack.pop()
                next_greater[curr] = i
            if len(stack) > 0:
                prev_greater[i] = stack[-1]
            stack.append(i)
        res = 0
        for i in range(n):
            if left <= nums[i] <= right:
                l = prev_greater[i]
                r = next_greater[i]
                res += (i - l) * (r - i)
        return res
class Solution {
    public int numSubarrayBoundedMax(int[] nums, int left, int right) {
        int res = 0;

        int s = -1;
        int e = -1;

        for(int i=0;i<nums.length;i++){

            if(nums[i] >= left && nums[i] <= right){
                e = i;
            }else if(nums[i] > right){
                e = s = i;
            }

            res += (e - s);
        }

        return res;
    }
}
class Solution {
public:
    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {
        int ans = 0; // store ans
        int j=-1; // starting window
        int sub = 0; // if current element is less than left bound then count how may element before current element which is less than left and must be continues(it means any element which is greater than left bound reset the count to 0 )
        for(int i=0;i<nums.size();i++){
            if(nums[i]>right){
                j = i;
                sub = 0;
            }
            else if(nums[i]<left){
                sub++;
            }
            else sub = 0;
            ans = ans + i - j - sub;
        }
        return ans;
    }
};
  var numSubarrayBoundedMax = function(nums, left, right) {
        // si is start index
        // ei is end index
    let si=0, ei=0, finalCount=0, currentCount=0;

    while(ei<nums.length){ // moving ei till length of array
        if(left<=nums[ei] && nums[ei]<=right) // considering case number falls in the range
            {
                currentCount= ei-si+1;  // to get the subarrays b/w start index and end index
                finalCount+=currentCount; // add current count of subarrays to final count  which we will return at end
            }
        if(nums[ei]<left){
              finalCount+=currentCount;  // in this case when num is smaller than left range we won't calculate subarrays but just include the current count in final count.
        }
         if(nums[ei]>right){
              si=ei+1; // in this case when num is greater than right range we need to move our start index to end+1 because there won't be any subarrays that will fall in range.
             currentCount=0; // we will set current count 0 in such case.
        }

        ei++;
     }
    return finalCount;

    };
Number of Subarrays with Bounded Maximum