Very simple Python3 and C++ with explanation
Whenever we come accross any counting subarray type of questions, the approach is to use two pointer and maintain a counter "num_subarrays" to keep the count of
Whenever we come accross any counting subarray type of questions, the approach is to use two pointer and maintain a counter "num_subarrays" to keep the count of number of subarrays.\nLet i, j be the two pointers, then total number of subarrays from i:j is `num_subarrays += (j-i+1)`. see below for eg.\n[1 2 3]\ni   |   j  | num_subarrays\n0  |  0  | 0 + (0-0+1) = 1\n0  |  1  | 1 + (1-0+1) = 3\n0  |  2  |  3 + (2-0+1) = 6\n\ntotal subarrays is 6!\n\nWe have to use the same strategy here whenever the currently element is between [L,R].\nIf the element is below L, then we add the previous count the subarray.\nIf the element is above R, then we change the i = j + 1 and reset the prev value.\n\n\nPython\n```\nclass Solution:\n    def numSubarrayBoundedMax(self, A: List[int], L: int, R: int) -> int:\n        i = j = count = prev = 0\n        while(j < len(A)):\n            if (L <= A[j] <= R):\n                prev = j - i + 1\n                count += prev\n            elif (A[j] < L):\n                count += prev\n            else:\n                i = j+1\n                prev = 0\n            j += 1\n        return count\n```\nC++\n```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n        if(A.empty()) return 0;\n        int i = 0, j = 0;\n        int count = 0;\n        int prev = 0;\n        while(j < A.size()){\n            \n            if ((L <= A[j]) && (A[j]<= R)){\n                prev = j - i + 1;\n                count += prev;\n                \n            }\n            else if (A[j] < L){\n                count += prev;\n            }\n            else{\n                i = j + 1;\n                prev = 0;\n            }\n            j++;\n        }\n        return count;\n    }\n};\n```\n
[]