Sort by

recency

|

2829 Discussions

|

  • + 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
    }
    
  • + 0 comments

    Java

        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
            int n = Integer.parseInt(bufferedReader.readLine().trim());
            
            StringBuffer strBase10 = new StringBuffer();
            int maxOnes = 0;
            int onesFound = 0;
            
            while(n>=1){
                double x = n;
                if(x%2==0){
                    strBase10.append("0");
                    if(onesFound>maxOnes){
                        maxOnes = onesFound;
                    }
                    onesFound = 0;
                }else{
                    onesFound++;
                    strBase10.append("1");
                }
                n = n/2;             
            }
            
            if(onesFound>maxOnes){
                maxOnes = onesFound;
            }
           
           System.out.println(maxOnes);
            
        }
    
  • + 0 comments

    JS:

    function main() {
        const n = parseInt(readLine().trim(), 10);
    
        console.log(n.toString(2).split('0').reduce((max, str) => Math.max(max, str.length), 0));
    }
    
  • + 0 comments

    c++: ` void d_b{int n){ int max= 0, count =0, rem =0; if (n == 0) cout << 0 << endl; else ( while (n>0){ rem = n%2; count = rem? count + 1: 0; max = (count> max) ? count:max; d/=2; } } cout << max << endl; } int main(){ ........... ......... d_b(n); return 0; }

  • + 0 comments
    import math
    import os
    import random
    import re
    import sys
    
    
    
    if __name__ == '__main__':
        n = int(input().strip())
        binary=bin(n)[2:]
        ones=binary.split('0')
        max_ones=max(len(group) for group in ones)
        print(max_ones)