C++ Solution in O(n)


#1

int Solution::solve(vector &A, int B) {
int i,j,ans=0,n,xo=0;
n=A.size();
int prefix[n];
for(i=0;i<n;++i) {
xo^=A[i];
prefix[i]=xo;
}
unordered_map<int,int> m;
for(i=0;i<n;++i) {
if(prefix[i]==B) ans++;
ans+=m[B^prefix[i]];
m[prefix[i]]++;
}
return ans;
}


#2

Please explain the logic for incrementing the value for the prefixes (m[prefix[i]]++;) as key but using B^prefixes to count the number of subarrays (ans+=m[B^prefix[i]]:wink: