Count Subarrays with XOR K
Count the subarrays whose elements XOR to exactly k.
Intuition
XOR has the same prefix trick as sums, because XOR is its own inverse: xor(i..j) = pre[j] ⊕ pre[i−1]. A subarray XORs to k exactly when the current prefix XOR equals some earlier prefix ⊕ k. Count how many earlier prefixes equal pre ⊕ k.
Approach
Prefix XOR replaces prefix sum
Keep running pre = a[0]^…^a[i]. Subarray (j..i] has XOR pre ⊕ pre_j. We want that to equal k, i.e. pre_j == pre ⊕ k.
Count occurrences, don't just remember one
Unlike the longest variant, every earlier matching prefix gives a distinct subarray — so the map stores value → count and we add the whole count.
Seed with {0: 1}
The empty prefix has XOR 0, letting subarrays that start at index 0 be counted.
Solution & live demo
Edge cases
Counts subarrays whose prefix XOR repeats — handled naturally by the same formula.
Counter accumulates; each earlier occurrence is a separate subarray.