Subarray with given XOR

 https://www.interviewbit.com/old/problems/subarray-with-given-xor/

int Solution::solve(vector<int> &A, int B) {
    int count=0;
    int Xor=0;
    map<int,int> freq;
    for(auto it:A){
        
        Xor= Xor^it; //prefix Xor
        
        if(Xor==B){
            count++;
        }
        
        if(freq.find(Xor^B)!=freq.end())  // Xor^B == y , y^Xor == B
        {
            count+=freq[Xor^B];
        }
        
        freq[Xor]++; //put in map the Xor
        
    }
    return count;
}

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!