✅ Number of Subarrays with Bounded Maximum | Easy Divide elements into  Buckets w/Explanation
Thought Process:\n\n1) There can be three cases for every element in the array.\n* element < left (Case 1)\n* element <= right (Case 2)\n* element > right (Case
**Thought Process:**\n```\n1) There can be three cases for every element in the array.\n* element < left (Case 1)\n* element <= right (Case 2)\n* element > right (Case 3)\n\n2) Case 1 and Case 2 are overlapping.\n* But we need to consider only that subarrays whose maximum element lies between [left,right].\n* This can be done by calculating all the possible candidaties with Case 1 and Case 2 seperately,\nand then Our required answer can be calculated as (Case2 - Case1).\n\n3) We will consider all the elements < left into bucket1,\n4) And all the elements <= right into bucket2,\n\n# NOTE: We need not consider Case 3.\n\n5) All the elements between range [left,right] can be calculated by taking difference of bucket2 and bucket1.\n\nOne Possible candidate  = bucket2-bucket1.\n\nAs we need to find all the possible candidates.\nSo we can sum up all the possible candidates answer\n```\n**Time Complexity**\n```\nO(N)\nwhere N = Number of elements in the array.\n```\n\n**Space Complexity:**\n```\nO(1) (Excluding the input array)\n```\n\n**C++:**\n```\nclass Solution {\n    public:\n        int numSubarrayBoundedMax(vector < int > & nums, int left, int right) {\n            int bucket1 = 0, bucket2 = 0;\n            int cnt = 0;\n            for (int ele: nums) {\n                bucket1 = (ele < left) ? bucket1 + 1 : 0;\n                bucket2 = (ele <= right) ? bucket2 + 1 : 0;\n                cnt += bucket2 - bucket1;\n            }\n            return cnt;\n        }\n};\n```\n\n**Python:**\n```\nclass Solution:\n    def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:\n        bucket1 = 0\n        bucket2 = 0\n        cnt = 0\n        for ele in nums:\n            if ele < left:\n                bucket1 += 1\n            else:\n                bucket1 = 0\n            if ele <= right:\n                bucket2 += 1\n            else:\n                bucket2 = 0\n            cnt += bucket2 - bucket1\n        return cnt        \n```\n\n**JavaScript:**\n```\n/**\n * @param {number[]} nums\n * @param {number} left\n * @param {number} right\n * @return {number}\n */\nvar numSubarrayBoundedMax = function(nums, left, right) {\n    bucket1 = 0;\n    bucket2 = 0;\n    cnt = 0;\n    nums.forEach(assignBuckets);\n    function assignBuckets(item, index)\n    {\n        if(item<left)\n            bucket1++;\n        else\n            bucket1=0;\n        if(item<=right)\n            bucket2++;\n        else\n            bucket2=0;\n        cnt += bucket2-bucket1;\n    }\n    return cnt;\n};\n```\n\n**JAVA:**\n```\nclass Solution {\n    public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n        int bucket1 = 0;\n        int bucket2 = 0;\n        int cnt = 0;\n        for(int ele:nums)\n        {\n            bucket1 = (ele<left)?bucket1+1:0;\n            bucket2 = (ele<=right)?bucket2+1:0;\n            cnt += bucket2-bucket1;\n        }\n        return cnt;\n    }\n}\n```\n\n**Swift:**\n```\nclass Solution {\n    func numSubarrayBoundedMax(_ nums: [Int], _ left: Int, _ right: Int) -> Int {\n        var bucket1 = 0;\n        var bucket2 = 0;\n        var cnt = 0;\n        for ele in nums{\n            bucket1 = (ele<left) ? (bucket1+1) : 0;\n            bucket2 = (ele<=right) ? (bucket2+1) : 0;\n            cnt += bucket2-bucket1;\n        }\n        return cnt;\n    }\n}\n```\n\n**GO:**\n```\nfunc numSubarrayBoundedMax(nums []int, left int, right int) int {\n    var bucket1 int = 0\n    var bucket2 int = 0\n    var cnt int = 0\n    for _,ele := range nums{\n        if ele<left{\n            bucket1++\n        }else{\n            bucket1 = 0\n        }\n        \n        if ele<=right{\n            bucket2++\n        }else{\n            bucket2 = 0\n        }\n        cnt += bucket2 - bucket1\n    }\n    return cnt\n}\n```\n**Kotlin:**\n```\nclass Solution {\n    fun numSubarrayBoundedMax(nums: IntArray, left: Int, right: Int): Int {\n        var bucket1 = 0\n        var bucket2 = 0\n        var cnt = 0\n        for(ele in nums)\n        {\n            bucket1 = if(ele<left) (bucket1+1) else 0\n            bucket2 = if(ele<=right) (bucket2+1) else 0\n            cnt += bucket2-bucket1\n        }\n        return cnt\n    }\n}\n```\n\n```\nPlease Upvote the solution if you find it useful thanks.\n```
[]