Beginner-Friendly Solution || Easy Video Explanation in Hindi
Intuition\r\nHashing + PrefixSum\r\n\r\n# Approach\r\n\r\nKindly check the video links given below for understanding both the prefix sum concept + actual soluti
# Intuition\r\nHashing + PrefixSum\r\n\r\n# Approach\r\n\r\nKindly check the video links given below for understanding both the prefix sum concept + actual solution. Detailed explanations are provided in Hindi.\r\n\r\nPrefix-Sum Concept(Pre-requisite) : www.youtube.com/watch?v=PX5uIJC4sXQ\r\n\r\nActual Solution : www.youtube.com/watch?v=C_y2PFLyDp8\r\n\r\n\r\n# Complexity\r\n- Time complexity: O(N)\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: O(N)\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# C++\r\n```\r\nclass Solution {\r\npublic:\r\n    int numberOfSubarrays(vector<int>& nums, int k) {\r\n        //Optimal Solution T.C.: O(N), S.C.: O(N)\r\n        int n = nums.size();\r\n\r\n        int i=0;\r\n\r\n        //Updating even to 0 and odd to 1\r\n        while(i!=n){\r\n            if(nums[i]%2!=0)\r\n            nums[i]=1;\r\n            else\r\n            nums[i]=0;\r\n            i++;\r\n        }\r\n\r\n\r\n        //Simple use count subarray with sum K problem logic.\r\n\r\n        int count =0;\r\n        unordered_map<int,int> map;\r\n        int sum=0;\r\n\r\n        i=0;\r\n        while(i!=n){\r\n            sum+=nums[i];\r\n            if(sum==k) count++;\r\n\r\n            if(map.find(sum-k)!= map.end()){\r\n                count+=map[sum-k];\r\n            }\r\n\r\n            map[sum]++;\r\n            i++;\r\n        }\r\n        return count;\r\n    }\r\n};\r\n\r\n```\r\n# Java\r\n```\r\nclass Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        //Optimal Solution T.C.: O(N), S.C.: O(N)\r\n        int n = nums.length;\r\n\r\n        int i=0;\r\n\r\n        //Updating even to 0 and odd to 1\r\n        while(i!=n){\r\n            if(nums[i]%2!=0)\r\n            nums[i]=1;\r\n            else\r\n            nums[i]=0;\r\n            i++;\r\n        }\r\n\r\n\r\n        //Simple use count subarray with sum K problem logic.\r\n\r\n        int count =0;\r\n        Map<Integer,Integer> map = new HashMap<>();\r\n        int sum=0;\r\n\r\n        i=0;\r\n        while(i!=n){\r\n            sum+=nums[i];\r\n            if(sum==k) count++;\r\n\r\n            if(map.containsKey(sum-k)){\r\n                count+=map.get(sum-k);\r\n            }\r\n\r\n            map.put(sum,map.getOrDefault(sum,0)+1);\r\n            i++;\r\n        }\r\n        return count;\r\n    }\r\n}\r\n```\r\n# Python3\r\n```\r\nclass Solution:\r\n    def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n        # Optimal Solution T.C.: O(N), S.C.: O(N)\r\n        n = len(nums)\r\n\r\n        # Updating even to 0 and odd to 1\r\n        i = 0\r\n        while i < n:\r\n            if nums[i] % 2 != 0:\r\n                nums[i] = 1\r\n            else:\r\n                nums[i] = 0\r\n            i += 1\r\n\r\n        # Simple use count subarray with sum K problem logic.\r\n        count = 0\r\n        num_map = {}\r\n        curr_sum = 0\r\n\r\n        i = 0\r\n        while i < n:\r\n            curr_sum += nums[i]\r\n\r\n            if curr_sum == k:\r\n                count += 1\r\n\r\n            if curr_sum - k in num_map:\r\n                count += num_map[curr_sum - k]\r\n\r\n            if curr_sum in num_map:\r\n                num_map[curr_sum] += 1\r\n            else:\r\n                num_map[curr_sum] = 1\r\n\r\n            i += 1\r\n\r\n        return count\r\n```\r\n\r\n## Please upvote if you like the solution.
['Array', 'Hash Table', 'Prefix Sum', 'C++', 'Java', 'Python3']