Home30 Days Of CodeHackerRank Day 10 : Binary Numbers 30 days of code solution

# HackerRank Day 10 : Binary Numbers 30 days of code solution

Today we are going to solve HackerRank Day 10 : Binary Numbers 30 days of code solution in C, C++ , Java , Python & Javascript.

Objective

Today, we’re working with binary numbers.

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.

Example

n = 125

The binary representation of 12510 is 11111012. In base 10, there are 5 and 1 consecutive ones in two groups. Print the maximum, 5.

#### Input Format

A single integer, n.

#### Constraints

• 1 <= n <= 106

#### Output Format

Print a single base-10 integer that denotes the maximum number of consecutive 1‘s in the binary representation of n.

Sample Input 1

``5``

Sample Output 1

``1``

Sample Input 2

``13``

Sample Output 2

``2``

Explanation

Sample Case 1:
The binary representation of 510 is 1012, so the maximum number of consecutive 1‘s is 1.

Sample Case 2:
The binary representation of 1310 is 11012, so the maximum number of consecutive 1‘s is 2.

## Binary Numbers HackerRank Solution in C

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

int main(){
int n,count=0,max=0;
scanf("%d",&n);
while(n>0)
{
if(n%2==1)
count++;
else
{
if(count>max)
max=count;
count=0;
}
n/=2;
}
if(count>max)
max=count;
printf("%d",max);
return 0;
}```

## Binary Numbers HackerRank Solution in C ++

```#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstring>
#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;
string toBinary(int n)
{
string r;
while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
return r;
}
int consOnes(string r, int size){
int max=0 ,p=0;
for(int i= 0; i<size;i++){
if(r.substr(i,1)=="1"){
p++;
if (p>max) max=p;
}
else{
p=0;
}
}
return max;
}
int main(){
int n;
cin >> n;
string r = toBinary(n);
cout << consOnes(r,r.length());

return 0;
}```

## Binary NumbersHackerRank Solution in Java

```import java.io.*;
import java.util.*;

public class Solution {

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

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);
}
}```

## Binary Numbers HackerRank Solution in Python 3

```#!/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)```

## Binary NumbersHackerRank Solution in JavaScript

```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();
});

return input_stdin_array[input_currentline++];
}

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

function main() {
var splits = n.split('0');
console.log(splits.map(function(elem){return elem.length;}).reduce(function(a,b){
if (a>b) return a; else return b;}));

}
```

30 Days of Code HackerRank Solutions List – Day 0 to Day 29

RELATED ARTICLES