Consecutive Ones in Binary Representation
Given a non-negative integer n, convert it to binary and find the maximum number of consecutive ones in its binary representation. Return this maximum count.
[ 5 ]
Explanation. The binary representation of 5 is 101, which contains a maximum of 1 consecutive one.
[ 15 ]
Explanation. The binary representation of 15 is 1111, which contains a maximum of 4 consecutive ones.
[ 0 ]
Explanation. The binary representation of 0 is 0, and there are no consecutive ones.
[ 16 ]
Explanation. The binary representation of 16 is 10000, which contains no consecutive ones.
[ 6 ]
Explanation. The binary representation of 6 is 110, which contains a maximum of 2 consecutive ones.
Follow-up: How would your solution change if the input were a binary string instead of an integer?
The input integer will be a non-negative integer.
- Views
- 4