Cpp hashmap solution


#1

#define ll long long int
int Solution::solve(vector &a, int k){
ll n = a.size() ;
map<ll, ll> cnt ;
ll xr=0, ans=0;
for(ll i=0; i<n; i++){
xr ^= a[i] ; // calculating prefix xor
if(xr == k) // if the calculated xor till now is the required subarray
ans++ ;
ll y = xr^k ;
if(cnt.find(y)!=cnt.end())
ans += cnt[y] ;
cnt[xr]++ ;
}
return ans ;
}