C++ + Well explained + Easy + Linear time
\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n\t\t// Sum will store the total number of subarrays\n        int sum
```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n\t\t// Sum will store the total number of subarrays\n        int sum = 0;\n\t\t// Count will store count of all the elements with value <= R\n        int count = 0;\n\t\t// X will store count of all the subarrays which has maximum array element at least L and at most R\n        int x = 0;\n        for( int i = 0 ; i < A.size() ; i++ ){\n            if( A[i] > R ){\n                count = 0;\n                x = 0;\n                continue;\n            }\n            count++;\n            if( A[i] >= L && A[i] <= R ){\n                sum += count;\n                x = count;\n                continue;\n            }\n\t\t\t// This is main why i am incrementing sum by x\n\t\t\t// Ex ->  1 , 2 , 1\n\t\t\t// This is to handle condition when my index is at 2, and i have to count subarray containing index 2.\n\t\t\t// At index 1, i have subrrays with indices (1) (0,1) which has maximum array element at least L and at most R\n\t\t\t// and  x = 2.\n\t\t\t// At index 2, my subarray includes with indices  (1,2), (0,1,2) which has maximum array  element at least L and at most R.\n\t\t\t// So i need to includes all the subarray when i encountered condition A[i] >= L && A[i] <= R.\n            sum += x;\n        }\n        return sum;\n    }\n};    \n```
[]