Hacker Rank - Day 10 : Binary Numbers

                        Day 10 :Binary Numbers

Solution In Java 8:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Long n_to_b = Long.parseLong(Integer.toBinaryString(n));
        int  max = 0, cnt = 0;
        Long n_to_b1 = n_to_b ;
        while(n_to_b1>0)
            {
            Long rem = n_to_b1 % 10;
            if(rem == 1)
                {
                cnt++;
            }
            else{
                cnt = 0;
            }
            if(cnt > max)
                {
                max = cnt;
            }
            n_to_b1 = n_to_b1 / 10;
        }
      
        System.out.print(max);
    }
}
Solution In Python 3: 


#!/bin/python3
no = int(input())
n_to_b = "{0:b}".format(no)
N_TO_B = str(n_to_b)
cnt = 0
maxi = 0
n = N_TO_B
for i in n:
    if(i == '1'):
        cnt=cnt+1
    else:
        cnt = 0
    if(cnt > maxi):
        maxi = cnt
print(maxi)      
Share:

0 comments:

Post a Comment