Python easy prefix sum solution
Hi guys,\n\nHere is a simple solution using the prefix sum of an array where each element is equal to 1 if nums[i]%2==1 and 0 if nums[i]%2==0. \n\nWe initialize
Hi guys,\n\nHere is a simple solution using the prefix sum of an array where each element is equal to 1 if nums[i]%2==1 and 0 if nums[i]%2==0. \n\nWe initialize the dictionary so that we take into account the arrays with k odd numbers starting at index 0.\n\n\t\tpref=list(accumulate([0 if num%2==0 else 1 for num in nums]))\n        dico=defaultdict(int)\n        res=0\n        dico[0]=1\n        for num in pref:\n            if num-k in dico:\n                res+=dico[num-k]\n            dico[num]+=1\n        return res\n\t\t\nThen if pref[i]==j it means we have j odd numbers from index 0 to index j. Hence if j-k is in our hash table, it means that for dico[j-k] indices, we had j-k odd numbers. So between these indices and index i, we have a total k odd numbers : j-(j-k)=k.\n\nPlease upvote if you liked!\n\nCheers,\nBerthouille
['Prefix Sum', 'Python']