Binary Numbers problem solution - 30 days of code HackerRank

Input Format
A single integer, .
Output Format
Print a single base- integer denoting the maximum number of consecutive 's in the binary representation of .
Binary Numbers problem solution - 30 days of code HackerRank

Problem solution in Java programming language.

import java.util.*;

public class Solution {
    public static int mostConsecutiveOnes(int n) {
        // convert number->binary string->char array
        char[] binary = Integer.toBinaryString(n).toCharArray();
        
        // count of current sequence of consecutive ones
        int tmpCount = 0; 
        
        // running maximum count of consecutive ones for any section to left of tmpCount
        int maxCount = 0; 
        for(int i = 0; i < binary.length; i++){
            
            // reset to 0 if we hit a '0' char
            if(binary[i] == '0') {
                
                // set new max if needed
                if(tmpCount > maxCount){
                    maxCount = tmpCount;
                }
                    
                tmpCount = 0;
            }
            else { // current location is a section of consecutive 1's
                // increment tmpCount
                tmpCount =  tmpCount + 1; 
            }
        }
        // conditional is necessary here in case the string does not end with a 0
        return (tmpCount > maxCount) ? tmpCount : maxCount;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();

        System.out.println(mostConsecutiveOnes(n));
    }
}

Second method

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
        
        // convert number->binary string->char array
        char[] binary = Integer.toBinaryString(n).toCharArray();
        int tmpCount = 0; // count consecutive ones
        int maxCount = 0; // running maximum of consecutive ones
        for(int i = 0; i < binary.length; i++){
         // reset to 0 if we hit a '0' char
            tmpCount = (binary[i] == '0') ? 0 : tmpCount + 1; 
            
            // set max
            if(tmpCount > maxCount){
                maxCount = tmpCount;
            }
        }
        System.out.println(maxCount);
    }
}

Third method

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        // convert to binary and split into strings of consecutive ones
        String[] groupings = Integer.toBinaryString(n).split("0");
        int max = 0;
        for(String s : groupings){
            if(max < s.length()){
                max = s.length();
            }
        }
        System.out.println(max);
    }
}

Fourth method

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        // convert to binary and split into strings of consecutive ones
        String[] groupings = Integer.toBinaryString(n).split("0");
        // sorting strings only composed of a single character ("1") orders them by length
        Arrays.sort(groupings);
        
        // print length of lexicographically last string
        System.out.println(groupings[groupings.length - 1].length());
    }
}

Problem solution in the python programming language.

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input())

    rmd = []
    
    while n > 0:
        rm = n % 2
        n = n//2
        rmd.append(rm)
    
    count,result = 0,0
    
    for i in range(0,len(rmd)):
        if rmd[i] == 0:
            count = 0
        else:
            count +=1
            result = max(result,count)
    
    print(result)

Post a Comment

0 Comments