Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

Count the number of subarrays having a given XOR

Given an array of integers arr[] and a number m, count the number of subarrays having XOR of their elements as m.

Note: This problem covers many concepts of subarray and xor.

Brute force

Hashing solution

int countSubarrayWithXOR(vector<int>& a, int b)
{
    map<int, int> freq;
    int cnt = 0, xorr = 0;
    for (auto x : a) {
        xorr = xorr ^ x;

        if (xorr == b)
            cnt++;

        if (freq.count(xorr ^ b))
            cnt += freq[xorr ^ b];

        freq[xorr] += 1;
    }
    return cnt;
}