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?
- A. 13
- B. 18
- C. 7
- 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
- From the ten numbers [3, 8, 12, 5, 7, 10, 1, 4, 9, 6], how many 3-element subsets have a sum divisible by 5?
- A car starts at position 0 with 10 litres of fuel. It must reach position 40; it uses one litre per unit of distance. Gas stations at…
- From [5, 9, 2, 14, 7, 3, 8], keep as many values as possible so that the largest kept value is at most twice the smallest kept value. What…
- How many integers x with 0 <= x <= 255 have a number of 1-bits that is divisible by 3? (Zero has zero 1-bits, which is divisible by 3.)
- Trains arrive at [900, 940, 950, 1100, 1500, 1800] and depart at [910, 1200, 1120, 1130, 1900, 2000] (matched train by train). A platform…
- Problem: Children stand in a line with ratings [1, 0, 2]. Each child gets at least one candy, and a child with a higher rating than a…