Number of Set Bits Optimized

Can we do the operation n = n&(n-1) and increase counter by 1 ach time until n is not less than o equal to 0? I think i will take time propertional to number of set bits!! Thank You :slight_smile:

yes pratik it is an optimized way to do and tc of this is b where b is no of set bits

Thanks for your clarification

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.