Data Structures & AlgorithmsModerateCAT · TCS NQT

Problem: How many integers from 1 to 50 have exactly two 1-bits in their binary representation?

  1. A. 10
  2. B. 15
  3. C. 6
  4. D. 21

Correct answer: B. 15

Numbers with two 1-bits below 64 number C(6, 2) = 15, and the largest, 48 = 110000, is still ≤ 50, so all 15 count. Using only 5-bit numbers gives C(5, 2) = 10, counting exactly one 1-bit gives 6, and C(7, 2) = 21 covers up to 127.

More Coding Interview Problems questions

Exams that ask this topic