Algorithm


Problem Name: 30 days of code - Day 10: Binary Numbers

Problem Link: https://www.hackerrank.com/challenges/30-binary-numbers/problem?isFullScreen=true

In this HackerRank in 30 Days of Code Day 10: Binary Numbers problem solution,

Objective
Today, we're working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!

Task
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

'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 (125)10 is (1111101)2. 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 <= 10**6

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 (5)10 is (101)2, so the maximum number of consecutive 1's is 1.

Sample Case 2:
The binary representation of (13)10 is (1101)1 so the maximum number of consecutive 1's is 2.

 

 

 

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


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

int main() {
    long int n;
    char bin_str[100];
    int i = 0;
    scanf("%ld",&n);
    int count = 0;
    int max_count = count;
    while(n>0){
        int temp = n%2;
        n = n/2;
        if(temp == 0)
        {
            count = 0;
        }else{
            count++;
        }
        if(max_count < c ount){
                max_count = count;
        }
    }
    printf("%d",max_count>;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int sum = 0;
    int max = 0;

    while (n > 0) {
        if (n % 2 == 1) {

            sum++;

            if (sum > max) max = sum;

        } else sum = 0;

        n = n / 2;
    }

    cout << max;

    return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


using System;

class Solution
{
    static void Main(String[] args)
    {
        var n = int.Parse(Console.ReadLine());

        var sum = 0;
        var max = 0;

        while (n > 0)
        {
            if (n % 2 == 1)
            {
                sum++;

                if (sum > max)
                    max = sum;
            }
            else sum = 0;

            n = n / 2;
        }

        Console.WriteLine(max);
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Golang Programming

Code - Golang Programming


package main

import (
	"fmt"
)

func main() {
	var n, reminder, count, max int

	fmt.Scan(&n)

	for n > 0 {
		reminder = n % 2
		if reminder == 1 {
			count++
		} else {
			count = 0
		}
		if count > max {
			max = count
		}
		n /= 2
	}
	fmt.Println(max)

}
Copy The Code & Try With Live Editor

#5 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

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

        int sum = 0, max = 0;
        while (n > 0) {
            if (n % 2 == 1) {
                sum++;
                if (sum > max) {
                    max = sum;
                }
            } else {
                sum = 0;
            }
            n = n / 2;
        }
        System.out.println(max);
    }
}
Copy The Code & Try With Live Editor

#6 Code Example with Javascript Programming

Code - Javascript Programming


function main() {
    const n = parseInt(readLine().trim(), 10);
    const binary = n.toString(2)
    var max_count = 0

    const arr = binary.split('0')

    arr.map((each) => {
        if (each.length > max_count) {
            max_count = each.length
        }
    })

    console.log(max_count)
}
Copy The Code & Try With Live Editor

#7 Code Example with Python Programming

Code - Python Programming


num = int(input())

result = 0
maximum = 0

while num > 0:
    if num % 2 == 1:
        result += 1
        if result > maximum:
            maximum = result

    else:
        result = 0

    num //= 2

print(maximum)
Copy The Code & Try With Live Editor

#8 Code Example with PHP Programming

Code - PHP Programming


 0 ) {
	if ( $n % 2 == 1 ) {
		$count++;
		if ( $count >= $min ) {
			$min = $count;
		}
	} else {
		$count = 0;
	}
	$n = $n / 2;
}
echo $min;

fclose($handle);
?>
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] HackerRank Javascript in 10 days - JS 10 days arithmetic operators solution in Javascript HackerRank
Next
[Solved] Day 11: 2D Arrays solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code