Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many index pairs i < j in the array [1, 3, 5, 7, 2, 6, 8] have a[i] + a[j] equal to a power of 2?

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

Correct answer: C. 4

A sum is a power of two exactly when x & (x-1) is zero, and small enough values can be checked pairwise or by hashing complements to each power.

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

Exams that ask this topic