Data Structures & AlgorithmsModerateCAT · TCS NQT Digital

Problem: How many integers x with 1 ≤ x ≤ 100 have an odd number of set bits in their binary representation AND are odd themselves?

  1. A. 51
  2. B. 50
  3. C. 25
  4. D. 26

Correct answer: C. 25

Brute force over 1..100 finds 25 numbers that are odd and have an odd popcount. Dropping the 'odd number' condition gives 51, dropping the popcount condition gives 50, and counting EVEN numbers with an odd popcount gives 26.

More Coding Interview Problems questions

Exams that ask this topic