O(n) - 8 line of code - short explanation
Intuition\nChange this question to: count of sub-arrays with sum of elements equal to k\n Describe your first thoughts on how to solve this problem. \n\n# Appro
# Intuition\nChange this question to: count of sub-arrays with sum of elements equal to k\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIf we treat all odd numbers as 1 and all even numbers as 0. This problem changes to count of total number of sub arrays with sum of its elements equal to k. \n\nThis is a standard problem, we generally use HashMap to solve this.\n\nTraverse in the array, store the frequecy of prefix sums in the map.\n\nIf you find a prefix sum for which difference between prefix sum and k has seen before, that means elements between those prefix sum ending indexes make a sub array that has total k odd elements. The frequency of that prefix sum - k will contribute to the answer.\n\nWe want to add all such possibilities, that\'s why we are storing frequencies, and using a hashmap instead of hashset.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:  O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) \n    {\n        int n=nums.size();     int pre=0, count=0;\n\n        unordered_map <int,int> mp;\n\n        for(int i=0; i<n; i++)\n        {\n            pre += nums[i]%2;\n\n            if(pre==k)  count++;\n\n            if( mp.count(pre-k) )   count += mp[pre-k];\n\n            mp[pre]++;\n        }\n\n        return count;\n    }\n};\n```
['C++']