Skip to main content

Subarray with given XOR

Problem

Given an array of integers A and an integer B.

Find the total number of subarrays having bitwise XOR of all elements equals to B.

Solution Approach

Expected Time complexity: O(n)O(n)

Click - to see solution code
int Solution::solve(vector<int> &A, int B) {
int n = A.size();
int xorr = 0;
unordered_map<int, int> mp;
mp[0] = 1;
int ans = 0;
for (int i = 0; i < n; i++) {
xorr ^= A[i];
if (mp.find(xorr ^ B) != mp.end()) {
ans += mp[xorr ^ B];
}
mp[xorr]++;
;
}
return ans;
}