[Cpp] Concept , explanation and dry run
For the element at each index we check below\n- If the current element is greater than R (A[i]>R) than we can not have any subarray ending with current element 
For the element at each index we check below\n- If the current element is greater than R (A[i]>R) than we can not have any subarray ending with current element and we keep track of the current index for the future.\n- If current element is withing the range of L<=A[i]<=R than all possible subarrays ending with current element is the difference of i and previous out of range element\n- If current element is smaller than L A[i]<L , then all posisble subarrays ending with this element must include previous valid element(Dry run will clear this further).\n\nDry Run.\n```\nEg: 1,2,5,3,4,1,2\nL=3\nR=4\n```\n\n```\nfor i=0 , 1 < L so doesn\'t matter\n```\n```\nfor i=1, 2 < L so doesnt matter\n```\n```\nfor i=2, 5>R so we save this index for future.we still could not get any subarray\n```\n```\nfor i=3, 3>=L and 3<=R so all possible subarray ending with 3 is all possible elements between previous invalid index and i\nsubarrays= (3)\nres=1\n```\n\n```\nfor i=4, 4>=L and 4<=R so all possible subarray ending with 4 is all possible elements between previous invalid index and i\nsubarrays: (3,4),(4)\nres=3\n```\n```\nfor i=5, 1<L so all possible subarrays ending with 1 must have atleast one valid element so it will be all possible elements between previous valid element(4 in this case) and invalid element(5 in this case).\nsubarrays: (4,1), (3,4,1)\nres=5 so far\n```\n\n```\nfor i=6, 2<L this is similar to above case so all possible subarray here are these\nsubarrays: (4,1,2),(3,4,1,2)\nres=7 so far.\n```\n\nSo the intuition is at each index , all possible subarrays is the count of elements between previous valid element and invalid element.\n\n```\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n        int res=0;\n        int n = A.size();        \n        int ind=-1,valid=-1;        \n        for(int i=0;i<n;i++){            \n                                    \n            if(A[i]>R){\n                ind=i;\n                valid=i;\n            }\n            else if(A[i]>=L && A[i]<=R)\n                valid = i;\n                        \n            res+= valid-ind;            \n        }\n        return res;\n    }\n};\n```\ntime: O(N)\nspace: O(1)
[]