Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

For the array a = [1, 4, 7, 2, 5, 6, 3], how many index pairs i < j satisfy a[i] XOR a[j] = 5?

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

Correct answer: A. 3

Store each value in a hash map and for each x look up x XOR 5, adding the earlier matches; each unordered pair is counted once. Using AND instead of XOR counts a different relation, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic