📌📌 C++ || Two Pointers || Faster || Easy To Understand || C++ Code
Using Two Pointer\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(1)\n\n\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int 
* ***Using Two Pointer***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(1)***\n\n```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n\n        int n = nums.size();\n\n        long long ans = 0;\n\n        // first of all count the total no. of subarrays which has maximum <= right\n\n        int i = 0;\n\n        while(i < n)\n        {\n            long long j = i;\n\n            while(j < n && nums[j] <= right)\n            {\n                j++;\n            }\n\n            // count will store the no. of elements in the subarray\n\n            long long count = j - i;\n\n            // total subarrays of size n will be (n * (n + 1)) / 2\n\n            ans += (count * (count + 1)) / 2;\n\n            // move pointer\n\n            i = j + 1;\n        }\n\n        // now decrement all the subarrays which has maximum < left\n\n        // follow the same process as above\n\n        i = 0;\n\n        while(i < n)\n        {\n            long long j = i;\n\n            while(j < n && nums[j] < left)\n            {\n                j++;\n            }\n\n            // count will store the no. of elements in the subarray\n\n            long long count = j - i;\n\n            // total subarrays of size n will be (n * (n + 1)) / 2\n\n            ans -= (count * (count + 1)) / 2;\n\n            // move pointer\n\n            i = j + 1;\n        }\n\n        return ans;\n    }\n};\n```
['Two Pointers', 'C', 'C++']