Loading section...
Prefix XOR: The Bitwise Sibling
Concepts: pyPrefixXOR, pyBitwiseAggregate, pyXORComplement
Prefix XOR is the exact same pattern as prefix sum, but with XOR instead of addition. It exploits the properties of XOR -- specifically that a XOR a = 0 and a XOR 0 = a -- to answer range XOR queries in O(1) and count subarrays with specific XOR values. This is less commonly asked than prefix sum, but it appears in DE interviews at companies dealing with bitwise aggregations, data encoding, or checksum computation. Knowing it demonstrates pattern depth. Range XOR Query The formula xp[j+1] ^ xp[i] works because XOR is its own inverse. Every element in arr[0..i-1] appears in both xp[j+1] and xp[i], so it XORs with itself and becomes 0. What remains is exactly XOR(arr[i..j]). This is the same cancellation principle as prefix sum subtraction, but for XOR. The formula and structure are identica