Python: Easy Explained, Linear time and constant space solution: Sliding Window Approach.
Idea: Keep increasing the size of the window, till any element \'x\' is nums is not found such that x > right.\nOnce x is greater than right, shift the start of
Idea: Keep increasing the size of the window, till any element \'x\' is nums is not found such that **x > right**.\nOnce x is greater than right, shift the start of the window to the element that occurs next after \'x\'.\n\nThere will be 3 possible cases:\n1. If a number is in the bound [left, right], we simply increment the count of the prefectly bound elements(count) and increment the answer because that element can stand-alone as a subarray.\n2. If a number is less than the left boundary, then it cannot stand alone but can stand with each of the prevoius #count elements in the subarray, so, increment the answer by adding count.\n3. If the number is greater then the right bound, then its time to create a fresh subarray for the next element onwards, so shrink the window and move the start of the window i = the current index(j) + 1 and make the count of perfectly bounded elements between [left, right] to 0.\n\nDry Run:\n![image](https://assets.leetcode.com/users/images/11980afc-8c9f-494d-8fbd-72050a4f29b5_1623918802.3379784.png)\n\nThe Full Code:\n```\nclass Solution:\n    def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:\n        i = 0\n        j = 0\n        n = len(nums)\n        \n        count = 0\n        ans = 0\n        \n        while i<=j and j<n:\n            if left <= nums[j] and nums[j] <= right:\n                ans += (j - i + 1)\n                count = (j - i + 1)\n            elif right < nums[j]:\n                i = j + 1\n                count = 0\n            else:\n                ans += count\n            j += 1\n        return ans\n ```\n Time = O(n) and Space = O(1).\n Here, i = start of window.\n j = end of window.\n count = number of element in the perfectly bounded subarray.\n ans = number of subarray satisfying the required condition.\n \n**Perfectly Bounded Subarray** means that the elements are in range [left,right] and present in the current window [i,j].
['Sliding Window', 'Python']