Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

Let P[0] = 0 and P[k] be the XOR of the first k elements of [3, 5, 6, 3, 1, 5, 2]. How many pairs of indices (i < j) among 0..7 have P[i] = P[j]?

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

Correct answer: C. 2

Equal prefix XORs P[i] = P[j] mean that the XOR of the subarray a[i..j−1] is 0. Count equal values in the prefix list with a hash map. Forgetting P[0] = 0 removes one of the pairs.

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

Exams that ask this topic