Simple Java Solution | O(n)
\nclass Solution {\n    public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n        int res = 0,n = nums.length,count = 0,dist = 0;\n        bo
```\nclass Solution {\n    public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n        int res = 0,n = nums.length,count = 0,dist = 0;\n        boolean flag = false;\n        for(int i  = 0;i<n;i++){\n            if(nums[i] <= right)\n                count++;\n            else{ \n                count = 0;\n                flag = false;\n                dist = 0;\n            }\n            if(nums[i] < left && flag == true){\n                dist++;\n                res += count-dist;\n            }\n            else if(nums[i] >= left && nums[i]<= right){\n                res += count;\n                flag = true;\n                dist = 0;\n            }\n        }\n        return res;\n    }\n}\n```
[]