Sort by

recency

|

2833 Discussions

|

  • + 0 comments

    A simple one line solution for Python 3

    #!/bin/python3
    if __name__ == '__main__':
        n = int(input().strip())
    # Solution
        print(max([*map(len, bin(n)[2:].split("0"))]))
    
  • + 0 comments

    Blazing fast solution: O(max_run_length) -> for 1001110001100 loop will just run 3 times:

    n=int(input())
    r=0
    while n:
        n &= n<<1
        r+=1
    print(r)
    
  • + 0 comments

    Here is Day 10: Binary Numbers solution in python, java, c++, c and javascript - https://programmingoneonone.com/hackerrank-day-10-binary-numbers-30-days-of-code-solution.html

  • + 0 comments

    C# solution:

    int max = Convert.ToString(n, 2).Split('0').Max(m => m.Length);
    
  • + 0 comments

    TypeScript binary converter function (The hard way lol)

    function binary_convert(n:number):string{
        let i:number = 0;
        let binary:string = '';
        let max:number = 0;
        
        
        while(Math.pow(2,i)<n){
            //Save the largest number for considering total length of binary
            max = i+1;
            i++;    
        }
        
        // console.log(i);
                
        // Divide it until every element is turned to binary (No remainder left)
        while(n>0 && i > 0){
                // Binary is larger or equal to remainder, modulo and add 1 to the binary string
                if(Math.pow(2,i-1)<= n){
                    n = n%Math.pow(2,i-1);
                    binary += '1';
                }
                // Number bigger than largest binary : skip the current binary
                else{
                   binary += '0'; 
                }
                
            // Continue to smaller binary
            i--;
            }
        
        // console.log('max  =' + max)
        
        // If no remainder left, Add zeroes at the right until full length
        while(binary.length < max){
            binary += '0';
        }
        // console.log('binary = ' + binary)
        return binary
    }