Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

How many integers from 1 to 100 inclusive have exactly two 1-bits in their binary representation?

  1. A. 15
  2. B. 21
  3. C. 28
  4. D. 7

Correct answer: B. 21

A number with two set bits is 2^a + 2^b with a > b. Bits up to 2⁶ are allowed, and even the largest, 64 + 32 = 96, is ≤ 100, so the count is C(7, 2) = 21. Stopping at bit 5 gives C(6, 2) = 15, using bit 7 gives 28, and 7 counts numbers with one set bit.

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

Exams that ask this topic