Data Structures & AlgorithmsModerateProduct Company SDE · ICPC regional

Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all subarrays.

Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all subarrays.
from functools import reduce
v=[4,6,2,1,7,3,5,1]
n=len(v)
print(sum(1 for i in range(n) for j in range(i+2,n) if reduce(lambda a,b:a^b,v[i:j+1],0)==0))
What is printed?
  1. A. 4
  2. B. 1
  3. C. 0
  4. D. 7

Correct answer: A. 4

A subarray has XOR 0 when its prefix XORs match, and the brute force checks each subarray directly. Testing the sum instead of the XOR is a different condition, and targeting XOR 1 is a misread. The length-three restriction is applied to the scan range, so it must start at i + 2.

More Coding Interview Problems questions

Exams that ask this topic