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?
- A. 51
- B. 50
- C. 25
- 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
- Problem: What is the minimum number of bit flips needed to convert 29 into 15 (flips of single bits in binary)?
- Problem: What is the largest power of 2 that divides 480 × 36?
- Problem: How many pairs (a, b) with 1 ≤ a < b ≤ 20 satisfy gcd(a, b) = 1?
- Problem: Find the last digit (units digit) of 7^(7^7) in decimal.
- Problem: How many subsets of {3, 5, 7, 11, 13, 17} have a sum equal to 24 (the empty set is excluded since its sum is 0)?
- Problem: In how many ways can the number 12 be written as an ordered sum of parts each equal to 1, 2 or 3 (so 1+2+... and 2+1+... are…