Easy O(n) solution with prefix Xor


#1

int Solution::solve(vector &A, int B) {
int cou = 0;
vector v(A.size());

for(int i = 0;i < A.size();i++)
{
    cou ^= A[i];
    v[i] = cou;
}
int cou1 = 0;
unordered_map<int,int> mp;
for(int i = 0;i < A.size();i++)
{
    int x1 = B ^ v[i];
    if(x1 == 0)
    {
        cou1++;
    }
    
    if(mp.count(x1) > 0)
    {
        cou1 += mp[x1];
    }
    
    mp[v[i]]++;
}
return cou1;

}


#2

can you explain logic of ur code ?


#3

It is same like counting of subarrays with 0 sum