After some modification it's just find number of sub-arrays with sum B


#1
int Solution::solve(vector<int> &A, int B)
{
    // Make a new array whose ith element will be 1 if A[i] is odd and
    // will be 0 if A[i] is even.
    
    // Now this problem is reduced to find the number of subarrays in the
    // new array with sum equal to B.
    vector<int>v(A.size());
    int sum=0,count=0;
    unordered_map<int,int>m;
    for(int i=0;i<A.size();i++)
    {
        v[i]=(A[i]%2?1:0);
        sum+=v[i];
        if(sum==B)
        {
            count++;
        }
        if(m.find(sum-B)!=m.end())
        {
            count+=m[sum-B];
        }
        m[sum]++;
    }
    return count;
}