All problems
Count Subarrays with XOR K
Problem
Count the contiguous subarrays whose XOR equals k.
Maintain counts of prefix XOR values; for each new prefix p, the number of earlier prefixes equal to p XOR k gives the subarrays ending here. Runs in O(n) time.
Hint
XOR is its own inverse, so the needed earlier prefix is prefix XOR k.
Test cases
case 1
in: [[4,2,2,6,4],6]
expected: 4
case 2
in: [[1,1,1],0]
expected: 2
case 3
in: [[1],1]
expected: 1
case 4
in: [[],0]
expected: 0
case 5
in: [[0],0]
expected: 1
Starter codePython
def subarrays_xor_k(nums, k):
# Your code here
pass5 test cases · run your code against them in the editor