Data Structures & AlgorithmsHard

How many index pairs i < j in 1 2 3 4 5 6 7 8 have A[i] XOR A[j] equal to a POWER OF TWO (1, 2, 4, 8, …)?

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

Correct answer: C. 9

A value is a power of two exactly when it has a single set bit, which is tested by v & (v−1) = 0 for v > 0. Using AND or sums, or only checking the smallest powers, gives other counts.

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

Exams that ask this topic