Skip to content

Subarray with given XOR

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 = 6
Output: 4
Explanation: 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 = 5
Output: 2
Explanation: The subarrays having XOR of their elements as 5 are [5] and [5, 6, 7, 8, 9].

Constraints:

  • 1≤1 \le length of the array ≤105\le 10^5
  • 1≤A[i],B≤1091 \le A[i], B \le 10^9

Checking every (start, end) pair costs O(n2)O(n^2). 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.

  1. Set freq[0] = 1 (the empty prefix), xorr = 0, ans = 0.
  2. For each element a of A:
    • xorr ^= a;
    • add freq[xorr ^ B] (if present) to ans;
    • increment freq[xorr].
  3. Return ans.

Example 1. Blue is the current element, xorr is the prefix XOR up to it, and freq holds the prefix XORs seen before it:

  1. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 1 of 7
  2. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 2 of 7
  3. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 3 of 7
  4. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 4 of 7
  5. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 5 of 7
  6. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 6 of 7
  7. Prefix XOR counting on A = [4,2,2,6,4], B = 6, step 7 of 7
7 steps. Swipe or scroll to move between them.
  • Time: O(n)O(n) — one pass with average O(1)O(1) hash-map operations.
  • Space: O(n)O(n) — the map holds at most n+1n + 1 distinct prefix XORs.
  • Subarray starting at index 0: found through the initial freq[0] = 1.
  • Many answers: the count can grow like n2/2n^2 / 2, 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 ans

Give it a try first

Attempt the problem on InterviewBit — the code will still be here.