Easy C++ solution || Simple approach with dry-run of example || O(N) time complexity
Intuition\nWe simply store the position of the current invalid number, i.e. the number with value greater than R and then subtract its position with the positio
# Intuition\nWe simply store the position of the current invalid number, i.e. the number with value greater than R and then subtract its position with the position of the current valid number and sum it over the array. \n`Example: nums = [1,2,5,3,4,1,2], L=3, R=4`\nInitially, ans = 0, left = -1 and right  = -1\n`For i=0, 1 < L so no change`\n`For i=1, 2 < L so no change`\n`For i=2, 5>R so we save this index for future. We still could not get any subarray`\n```\nFor i=3, 3>=L and 3<=R so all possible subarray ending with 3 is all possible elements between previous invalid index and i\nsubarrays= (3)\nans=1\n```\n```\nFor i=4, 4>=L and 4<=R so all possible subarray ending with 4 is all possible elements between previous invalid index and i\nsubarrays: (3,4),(4)\nans=3\n```\n```\nFor i=5, 1<L so all possible subarrays ending with 1 must have atleast one valid element so it will be all possible elements between previous valid element(4 in this case) and invalid element(5 in this case).\nsubarrays: (4,1), (3,4,1)\nans=5 so far\n```\n```\nFor i=6, 2<L this is similar to above case so all possible subarray here are these\nsubarrays: (4,1,2),(3,4,1,2)\nans=7 so far.\n```\n\n# Code\n```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int L, int R) {\n        int ans = 0, left = -1, right = -1;\n        for (int i = 0; i < nums.size(); i++) \n        {\n            if(nums[i] > R) \n                left = i;\n            if(nums[i] >= L) \n                right = i;\n            ans += right-left;\n        }\n        return ans;\n    }\n};\n```
['Two Pointers', 'C++']