Solution
C++ []\n#pragma GCC optimize("Ofast","inline","-ffast-math")\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\n\nclass Solution {\npublic:\n    int numSubarrayBoun
```C++ []\n#pragma GCC optimize("Ofast","inline","-ffast-math")\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\n\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n        ios::sync_with_stdio(false);\n        cin.tie(0);\n        cout.tie(0);\n        \n        int i = 0, t1 = -1, t0 = -1, res = 0;\n        for(int &x : nums) {\n            if(x > right) t1 = i;\n            if(x >= left) t0 = i;\n            \n            res += t0 - t1;\n            ++i;\n        }\n        return res;\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def numSubarrayBoundedMax(self, A: List[int], left: int, right: int) -> int:\n        dp = 0\n        res = 0\n        prev = -1\n        for i,n in enumerate(A):\n            if n > right:\n                dp = 0\n                prev = i\n            elif left <= n <= right:\n                dp = i - prev\n                res += dp\n            else:\n                res += dp\n        return res\n```\n\n```Java []\nclass Solution {\n    public int numSubarrayBoundedMax(int[] nums, int left, int right)\n    {\n        int lb = -1, rb = -1;\n        int max = Integer.MIN_VALUE;\n        int total = 0;\n        int count = 0;\n        while(rb < nums.length - 1)\n        {\n            rb++;\n            int num = nums[rb];\n\n            if(num >= left && num <= right)\n                count = rb - lb;                       \n            else if(num > right)\n            {\n                count = 0;\n                lb = rb;\n            }\n            total += count;\n        }\n        return total;\n    }\n}\n```\n
['C++', 'Java', 'Python3']