We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 10: Binary Numbers
  5. Discussions

Day 10: Binary Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 2652 Discussions, By:

recency

Please Login in order to post a comment

  • julia_13
    7 hours ago+ 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    
    
    if __name__ == '__main__':
        n = int(input().strip())
        binary=[]
        while n>0:
            remainder=n%2
            if remainder==0:
                n=n/2
            else:
                n=(n-1)/2
            binary.append(remainder)
        binary.reverse()
    
        maxsum=1
        k=[]
    
        for i in range(len(binary)-1):
            if (binary[i]==binary[i+1]):
                maxsum=maxsum+1
            else:
                k.append(maxsum)
                maxsum=1
            if (binary[len(binary)-2]==binary[len(binary)-1]):
                k.append(maxsum)
        print(max(k))
    
    0|
    Permalink
  • ManuelBaez
    1 day ago+ 0 comments

    Hello everyone this is my solution in python3

    def to_binary(n):
        if n == 0:
            return "0"
        elif n == 1:
            return "1"
        else:
            return to_binary(n//2) + str(n % 2)
    
    def cont(string, curr=0, res=0):
        if not string:
            return res
        if string[0]=='1':
            curr += 1
        else:
            res = max(curr, res)
            curr = 0
        res = max (curr, res)
        return cont(string[1:], curr, res)
    
    
    
        
    def main():
        n = int(input().strip())
        str1 = to_binary(n)
        larg = int(len(str1))
        #print (str1)
        print(cont(str1))    
        
        return 0
    
    if __name__ == '__main__':
        main()
    
    0|
    Permalink
  • ManuelBaez
    2 days ago+ 0 comments

    Hello everyone this is my C solution.

    int main()
    {
        int n = parse_int(ltrim(rtrim(readline())));
        int consec = 0;
        int max = 0;
        while(n>0){
            if ((n%2)==1) {
                consec++;
            }
            else{
                if (consec>max) max =consec;
                consec = 0;
            }
            n /= 2;
        }
        consec>max ? printf("%d \n", consec):printf("%d \n", max);
        
        return 0;
    }
    
    0|
    Permalink
  • lebaodai2611
    6 days ago+ 0 comments

    My Python 3 code:

    if __name__ == '__main__':
        n = int(input().strip())
        s_group = bin(n)[2:].split('0')
        n_group = [len(x) for x in s_group]
        
        print(max(n_group))
    
    0|
    Permalink
  • prkasundra
    1 week ago+ 0 comments
        char[] binary = Integer.toBinaryString(n).toCharArray();
        int tmpCount = 0, int maxCount = 0;
        for(int i = 0; i < binary.length; i++){
            tmpCount = (binary[i] == '0') ? 0 : tmpCount + 1; 
            if(tmpCount > maxCount){
                maxCount = tmpCount;
            }
        }
        System.out.println(maxCount);
    
    0|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy