Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

How many integers x with 1 <= x <= 30 have a binary representation with no two adjacent 1-bits?

  1. A. 13
  2. B. 18
  3. C. 7
  4. D. 12

Correct answer: D. 12

5-bit strings with no adjacent 1s number F(7) = 13 including 0, and 31 = 11111 is excluded anyway, so 1..30 gives 12. Including 0 gives 13, counting the numbers that DO contain adjacent 1s gives 18, and stopping at 4-bit numbers (1..15) gives 7.

More Coding Interview Problems questions

Exams that ask this topic