Skip to main content

Day 10: Binary Numbers | HackerRank | solution in python


Objective

Today, we're working with binary numbers.

Task
Given a base-  integer,  , convert it to binary (base- ). Then find and print the base-  integer denoting the maximum number of consecutive  's in  's binary representation. When working with different bases, it is common to show the base as a subscript.

Example

The binary representation of   is  . In base  , there are   and   consecutive ones in two groups. Print the maximum,  .

Input Format

A single integer,  .

Output Format

Print a single base-  integer that denotes the maximum number of consecutive  's in the binary representation of  .

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Explanation

Sample Case 1:
The binary representation of   is  , so the maximum number of consecutive  's is  .

Sample Case 2:
The binary representation of   is  , so the maximum number of consecutive  's is  .


#PROGRAM IN PYTHON
import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input().strip())
    maxi=0
    current=0
    while(n>0):
        
        if n%2==1:
            current+=1
            if current > maxi:
                maxi=current
        
            
        else:
            current=0
        n//=2
print(maxi)

Comments