C++ : Sliding Window || Two pointers || O(n) Space
This question is slightly based on sliding window, however one needs proper idea of whats going on.\nWe actually just need to maintain the condition of (Oddnumb
This question is slightly based on sliding window, however one needs proper idea of whats going on.\nWe actually just need to maintain the condition of (Oddnumbers==k), once we get it equal.\nTill the time our odd number count is less than k, we just simply keep updating **"end"**.\nOnce our oddnum count is equal to k, we start increamenting **"start"**, till the time we reach an odd number(this actually gives us the number of nice subarrays till that end point, let it be **"cnt"**).\nAfter this , we continue our quest till we get our (k+1)th odd number but before that each even number means that we again have cnt number of nice subarrays possible so we keep updating total.\nOnce we get an odd number , we increament **"start"**, reduce oddnumber count by 2(because we do not increament end , so just reducing count by 1 would lead us to take the new odd number more than once and an infinte loop would start).\nSo , like this the process continues.\n\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n        int start=0,end=0;\n        int count=0, total=0, cnt=0;\n        while(end<nums.size())\n        {\n            if(nums[end]%2!=0)\n                count++;\n            if(nums[end]%2==0 && count==k)\n               { total+=cnt;end++;continue;}\n            if(count<k)\n                end++;\n            else if(count==k)\n            {\n             while(count==k)\n             {\n                 cnt++;\n                 if(nums[start]%2==0)\n                 {start++;continue;}\n                 else\n                 {break;}\n                 \n             }\n                total+=cnt;\n                \n                end++;\n            }\n            else if(count>k)\n            {\n                start++;\n                cnt=0;\n                count-=2;\n               \n               \n                \n            }\n         \n        }\n        return total;\n    }\n};\n\n```\nThis is the code !!\nPlease upvote if you find it even slightly helpful !! : )\n
['Two Pointers', 'C', 'Sliding Window', 'C++']