🔥2 Approaches || ⚡Easiest & Best Code in C++ with explanation ✅
Please Upvote if u liked my Solution\uD83D\uDE42\n# Using PrefixSum with HashMap(O(n) SC):-\n\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>&
## **Please Upvote if u liked my Solution**\uD83D\uDE42\n# Using PrefixSum with HashMap(O(n) SC):-\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n        int size=nums.size(),i=0,j=0;\n        for(auto &it:nums){\n            if((it & 1)==0)\n                it=0;\n            else           //Replacing all the even nos. with 0 & odd nos. with 1 \n                it=1;\n        }\n       //Now simply to get the ans we have to return the no of subarrays with sum = k\n        unordered_map<int,int> mp;\n        int prefixSum=0,ans=0;\n        for(auto it:nums){\n            prefixSum+=it;\n            if(prefixSum==k)\n                ans++;\n            if(mp.find(prefixSum-k)!=mp.end())\n                ans+=mp[prefixSum-k];\n            mp[prefixSum]++;\n        }\n        return ans;\n    }\n};\n```\n## Using Three Pointers(O(1) SC):-\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n        int n = nums.size(), i = 0, j = 0, h = -1, ans = 0, cnt_odd = 0;\n        while(j < n){\n            if(nums[j] & 1){\n                i = j;\n                break;\n            }\n            j++;\n        }\n        while(j < n){\n            if (nums[j] & 1)\n                cnt_odd++;\n            if (cnt_odd > k){\n                h = i;\n                while(cnt_odd > k || !(nums[i] & 1)){\n                    if(nums[i] & 1)\n                        cnt_odd--;\n                    i++;\n                }   \n            }\n            if(cnt_odd == k)\n                ans += i - h;\n            j++;\n        }\n        return ans;\n    }\n};\n```\n**Happy LeetCoding\uD83D\uDCAF\nPlease Upvote**\uD83D\uDE42
['Array', 'Hash Table', 'Math', 'C', 'Sliding Window', 'Prefix Sum']