Why this one is not giving correct output?

Why this one is not giving correct output?
I want to calculate consecutive 1s in a number.

Hey @cbcao263,

Can you give an example?
It will be helpful in understanding the question.

Day 10 Hackerrank 30 Days of Code


Sir Please explain the test cases too.
Like in 5 we have 101, So Consecutive 1s are 0 so I think 0 should be the answer

Hello @cbcao263,

No, it should be 1.

You have to count the maximum number of consecutive 1’s in the binary representation of a number.

Examples:

  1. 5 : 101
    there is a single 1 together at once.
    So, output is 1.

  2. 6: 110
    there are two 1 together at max.
    So, output is 2:

  3. 22: 10110
    There are two 1 together at max.
    So, output is 2.

Correct Logic:


I just wrote this code and it is passing all the test cases.

I would suggest you to first finish the lectures on bitmasking in your course if you haven’t.
The above code is based on learning from those lectures.

Hope, this would help.
Give a like if you are satisfied.

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.