Count Subarrays With XOR K
Python
Hard
5 views
Problem Description
For each testcase you get n, k and n integers. Count number of subarrays whose XOR is exactly k.
Input Format
First integer t. For each: n k then n ints.
Output Format
t lines counts.
Constraints
Sum of n over all testcases
Official Solution
import sys
from collections import defaultdict
p=sys.stdin.read().strip().split()
if not p:
sys.exit(0)
it=iter(p)
t=int(next(it))
out=[]
for _ in range(t):
n=int(next(it)); k=int(next(it))
freq=defaultdict(int)
freq[0]=1
px=0
ans=0
for i in range(n):
x=int(next(it))
px^=x
ans+=freq[px^k]
freq[px]+=1
out.append(str(ans))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!