Easy C++ stack solution || commented
\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n     \n        //get the number of elements that are small
```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n     \n        //get the number of elements that are smaller to the left and right of every element in nums\n\t\t/by calculating this we can get number of subarrays that can be formed with the ele as a maximum and in range of [left,right]\n        \n        stack<int> s;\n        //stack stores the indices\n        int n=nums.size();\n        //calculate the number of elements that are smaller or equal  to right for every ele in nums\n        vector<int> smallToRight(n);\n        for(int i=n-1;i>=0;i-- )\n        {\n            while(!s.empty() && nums[s.top()]<=nums[i])\n                s.pop();\n            if(s.empty())\n                smallToRight[i]=n-i-1;\n            else\n                smallToRight[i]=s.top()-i-1;\n            s.push(i);\n        }\n        \n        //calculate the number of elements that are strictly less than it for every element in nums\n        \n        stack<int> s2;\n        \n        vector<int> smallToLeft(n);\n        for(int i=0;i<n;i++)\n        {\n            while(!s2.empty() && nums[s2.top()]<nums[i] )\n                s2.pop();\n            if(s2.empty())  //all the elements to left of i are small\n                smallToLeft[i]=i;\n            else\n                smallToLeft[i]=i-s2.top()-1;\n            s2.push(i);\n        }\n        \n        int res=0;\n        //now calculate the number of  subarrays that would possible if we consider the ele as max and it is in range of [left,right]\n        for(int i=0;i<n;i++)\n        {\n            if(nums[i]>=left && nums[i]<=right)\n            {\n                //get the number of subarrays possible\n                int possible=smallToLeft[i]+smallToRight[i] + smallToLeft[i]*smallToRight[i] + 1; \n                res+=possible;\n            }\n        }\n        return res;\n        \n    }\n};\n```\nFor those who are being confused by why we are considering only number which are strictly less than the element in smallToLeft[I] is \xA0not to calculate the duplicate subarrays if a ele is present twice. \n\nex- [1,1,2,1,2,1]\nwe should not consider the subarray [2, 1,2] two times if left=2 and right =3! \n**Please upvote if this helps you :)**
[]