Let L and R represent two positive integers, then we are required to find all the numbers in [L, R], i.e. L and R inclusive, that have 101 in their binary representation. For example, let L = 3 and R = 20 then 5(101) and 13(1101) are two such numbers.
Constraint
1 <= L <= R <= 10^18
My approach-
One obvious approach is to traverse from L to R and obtain the binary representation of each number and check if it contains 101, but that would take too much time given the constraint. Another way can be using Digit Dynamic Programming, more formally, if we can compute a function F(x), where x is a non negative integer and F(x) denotes the number of +ve integers smaller than or equal to x that satisfy the condition given in the question(i.e. it's binary representation contains 101) and if we assume that F(0) = 0 then the problem simply boils down to F(R) - F(L - 1). I have solved some Digit DP problems but unable to formulate this one, or maybe there is some other approach?