Is the logic efficient?

just wanted to know if there is a better approach to the problem

hello @granthdhir
yes there is better approach.

first get count of all bits in range [1…b] (say ans1)
the get count of all bits in range [1…a-1] (say ans2)
then ans1-ans2 will be the required answer.

now to get count of all the bits in range [1…n] refer this approach ->link

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.