Data Structures & AlgorithmsModerate

How many contiguous subarrays of [5, 2, 7, 1, 4, 6] have bitwise XOR exactly 3? Use prefix XOR values p[0]=0, p[i]=B[0]^…^B[i−1].

  1. A. 1
  2. B. 2
  3. C. 4
  4. D. 3

Correct answer: B. 2

A subarray (i, j] has XOR p[i] XOR p[j], so the count is the number of index pairs i < j with that XOR. Leaving out p[0] = 0 loses every subarray that starts at the first element, and counting ordered pairs doubles the answer.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic