Java Explained Solution || O(1) Space || 2 Pointers || Similar Question
There are 3 Possible Cases :\n1.  A[i] is between L and R (inclusive)\n2. A[i] is less than L\n3. A[i] is greater than R\n\nCASE 1: L<=A[i]<=R\nIt means that I(
There are 3 Possible Cases :\n1.  A[i] is between L and R (inclusive)\n2. A[i] is less than L\n3. A[i] is greater than R\n\n**CASE 1: L<=A[i]<=R**\n*It means that I(current element) can be part of previous subarrays (i-j) and also Can start a subarray from me (+1)\nSo add (i-j+1) in total Subarrays*\n\n**CASE 2: A[i]<L**\n*It means that I(current element) can be part of previous  subarrays(contigous) but cannot start subarray from me.\nSo add (i-j) in total subarrays(ans)*\n\n**CASE3: A[i]>R**\n*It means that I cannot be part of previous subarrays and also cannot start subarray from me.\nAs its subarray so put j=i+1 . now valid subarrays are going to start from next to me.* \n\n```\npublic int numSubarrayBoundedMax(int[] A, int L, int R) {\n        int i=0;\n        int j=0;\n        int ans=0;\n        int smaller=0;\n        \n        while(i!=A.length){\n            \n            if(A[i]>=L && A[i]<=R){\n                smaller=i-j+1;\n                ans+=smaller;\n            }else if(A[i]<L){\n                ans+=smaller;\n            }else{\n                j=i+1;\n                smaller=0;\n            }\n            i++;\n        }\n        return ans;\n    }\n```\n**Time:O(n) and Space:O(1)**\nPlease **upvote** if found it helpful :)\nSimilar Question : [Subarray product less than k](https://leetcode.com/problems/subarray-product-less-than-k/)
['Two Pointers', 'Java']