Day 10 - Binary Numbers

November 09, 2017


Task

Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1’s in n’s binary representation.

Input Format

A single integer, n.

Constraints

Output Format

Print a single base-10 integer denoting the maximum number of consecutive 1’s in the binary representation of n.

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Explanation

Sample Case 1:

The binary representation of 5 is 101, so the maximum number of consecutive 1’s is 1.

Sample Case 2:

The binary representation of 13 is 1101, so the maximum number of consecutive 1’s is 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import sys

n = int(input().strip())

bin = []
count = 0
maxCount =0

while n > 0:
    bin.append(n%2)
    n = n // 2

for i in bin:
    if i == 0:
        count = 0
    else:
        count += 1
    if maxCount < count:
        maxCount = count
print(maxCount)