Problem
Section titled “Problem”Given an array of integers A and an integer B.
Find the total number of subarrays having bitwise XOR of all elements equal to B.
The first argument is the integer array A, the second is the integer B. Return the total number of subarrays having bitwise XOR equal to B.
Example 1:
Input: A = [4, 2, 2, 6, 4], B = 6Output: 4Explanation: The subarrays having XOR of their elements as 6 are: [4, 2], [4, 2, 2, 6, 4], [2, 2, 6], [6].Example 2:
Input: A = [5, 6, 7, 8, 9], B = 5Output: 2Explanation: The subarrays having XOR of their elements as 5 are [5] and [5, 6, 7, 8, 9].Constraints:
- length of the array
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Checking every (start, end) pair costs . Prefix XORs turn the question into a lookup.
Let xorr be the XOR of A[0..i] and p the XOR of some shorter prefix A[0..j-1]. Because x ^ x = 0, the XOR of the subarray A[j..i] is xorr ^ p. That equals B exactly when
p = xorr ^ B.
So the number of good subarrays ending at i is the number of earlier prefixes whose XOR is xorr ^ B. If we keep a hash map freq counting how often each prefix XOR has occurred so far, each step is a single lookup. The empty prefix (XOR 0) is counted once at the start so that subarrays beginning at index 0 are found too.
Algorithm
Section titled “Algorithm”- Set
freq[0] = 1(the empty prefix),xorr = 0,ans = 0. - For each element
aofA:xorr ^= a;- add
freq[xorr ^ B](if present) toans; - increment
freq[xorr].
- Return
ans.
Walkthrough
Section titled “Walkthrough”Example 1. Blue is the current element, xorr is the prefix XOR up to it, and freq holds the prefix XORs seen before it:
Complexity
Section titled “Complexity”- Time: — one pass with average hash-map operations.
- Space: — the map holds at most distinct prefix XORs.
Edge cases
Section titled “Edge cases”- Subarray starting at index 0: found through the initial
freq[0] = 1. - Many answers: the count can grow like , so it is accumulated in a 64-bit integer to avoid signed overflow; the judge’s return type is
int.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
int Solution::solve(vector<int> &A, int B) { unordered_map<int, int> freq; // prefix XOR -> number of prefixes with that XOR freq[0] = 1; int xorr = 0; long long ans = 0; for (int a : A) { xorr ^= a; auto it = freq.find(xorr ^ B); if (it != freq.end()) ans += it->second; freq[xorr]++; } return ans;}public class Solution { public int solve(ArrayList<Integer> A, int B) { HashMap<Integer, Integer> freq = new HashMap<>(); // prefix XOR -> number of prefixes with that XOR freq.put(0, 1); int xorr = 0; long ans = 0; for (int a : A) { xorr ^= a; ans += freq.getOrDefault(xorr ^ B, 0); freq.merge(xorr, 1, Integer::sum); } return (int) ans; }}from collections import defaultdict
class Solution: def solve(self, A, B): freq = defaultdict(int) # prefix XOR -> number of prefixes with that XOR freq[0] = 1 xorr = ans = 0 for a in A: xorr ^= a ans += freq.get(xorr ^ B, 0) freq[xorr] += 1 return ansGive it a try first
Attempt the problem on InterviewBit — the code will still be here.
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 1 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/01.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 2 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/02.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 3 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/03.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 4 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/04.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 5 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/05.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 6 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/06.webp)
![Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 7 of 7](/CodePrime/img/codes/subarray-with-given-xor/prefix-xor/07.webp)