Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Day 10 Binary Numbers 30 days of code solution

YASH PAL, 31 July 2024

In this HackerRank Day 10 Binary Numbers 30 days of code problem set, we need to develop a program that can accept integer as an input and then convert it into a binary number and then into in base 10 integer. we need to print the base 10 integer that denotes the maximum number of consecutive 1’s in the binary representation of the input.

Day 10 Binary Numbers 30 days of code solution Hackerrank

Problem solution in Python 2 programming.

#!/bin/python

import sys

n = int(raw_input().strip())
binary = str(bin(n))
#print binary
tot=0
tmp=0
for i in binary:
    if i=='1':
        tmp+=1
    else:
        tot=max(tot,tmp)
        tmp=0
tot=max(tot,tmp)
print tot

Problem solution in Python 3 programming.

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

Problem solution in java programming.

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

Problem solution in c++ programming.

#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;
}

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

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()).toString(2);
    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 coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes