f
of size 32, where f[i]
will store the count of i
-bit binary numbers without consecutive ones.f[0]
to 1 and f[1]
to 2, as base cases for the Fibonacci sequence.f
array using the recurrence relation f[i] = f[i-1] + f[i-2]
for i
from 2 to 31.sum
to 0, which will store the final count of numbers without consecutive ones.prev_bit
to 0, which will track the previous bit in n
.n
from MSB to LSB using a loop.f[i]
to sum
, where i
is the index of the current bit from the LSB.
b. If prev_bit
is also 1, break the loop as we've encountered consecutive ones.prev_bit
to the current bit.sum
to include n
itself.sum
as the final count.