In this HackerRank Day 10: Binary Numbers problem solution Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation. When working with different bases, it is common to show the base as a subscript.

HackerRank Day 10: Binary Numbers problem solution

Problem solution in Python programming.

#!/bin/python3

import sys


n = int(input().strip())
binary_n = "{0:b}".format(n)

binary_string = str(binary_n)

max_ones_so_far = 0
max_ones = 0
for i in range(len(binary_string)):
    if binary_string[i] == '1':
        max_ones = max_ones + 1
    else:
        max_ones_so_far = max(max_ones_so_far, max_ones)
        max_ones = 0 
max_ones_so_far = max(max_ones_so_far, max_ones)
print(max_ones_so_far)



Problem solution in Java programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Long longn = Long.valueOf(n);
        
        String binary = Long.toBinaryString(longn);
        
        int maxLength = 0; int currLength = 0;
        
        for(int i=0; i<binary.length(); i++)
        {
            if(binary.charAt(i) == '1')
            {
                currLength++;
            }
            else
            {
                if(currLength >= maxLength)
                {
                    maxLength = currLength;
                    currLength = 0;
                }
                currLength = 0;
            }
        }
        
        if(currLength > maxLength)
        {
            maxLength = currLength;
        }
         
        System.out.println(maxLength);
    }
}




Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main(){
    int n;
    cin >> n;
    int ones = 0;
    int remainder=0;
    int maxones=0;
    vector<int> bin;
    while(n)
    {
        remainder = n%2;
        if(remainder&0x01)
            ones++;
        else
        {
            ones = 0;
        }
        maxones = max(ones, maxones);
        n= n/2;
        //cout << "CountOne : "<<ones << ":  " << maxones << "Max" << endl;
    }
    cout << maxones;
    return 0;
}



Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int exponent (int num, int expon){
    int w = 1;
    while (expon > 0){
        expon = expon - 1;
        w = w * num;
    }
    return w;
}

int main(){
    int n, x = 0; 
    char store[50] = "", store2[49] = "";
    scanf("%d",&n);
    
    while (n != 0){
        x = x + 1;
        if (exponent(2, x) >= n){
            n = n - exponent(2, (x - 1));
            strcat(store, "1");
            x = x - 1;
            while (x != -1){
                x = x - 1;
                if (exponent(2, x) <= n){
                    n = n - exponent(2, x);
                    strcat(store, "1");
                }
                else{
                    strcat(store, "0");
                }
            }
            break;
        }
    }
    strncpy(store2, store, strlen(store) - 1);
    
    int y = 0, z = 0, w = 0;
    z = strlen(store2);
    
    while(z != 0){
        z = z - 1;
        if (store2[z] == 49){
            y = y + 1;
            if (w < y){
                w = y;
            }
        }
        else{
            y = 0;
        }
    }
    printf("%d",w);
    return 0;
}




Problem solution in Javascript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    var ones = outputOnes(n);
    console.log(ones);
}

function outputOnes(num) {
    var binary = parseInt(num, 10).toString(2);
    var ones = 0;
    
    binary = binary.split("0");
    one = binary[1];

    for (var i = 0; i < binary.length; i++) {
        if(binary[i].length > ones) {
            ones = binary[i].length;
        }
    }
    
    return ones;
}