Type Here to Get Search Results !

Day 10: HackerRank 30 Days Of Code Solution By CodingHumans | Binary Numbers |

0

Day 10: Binary Numbers

Problem

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

Task
Given a base- 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.

Input Format

A single integer, n.

Constraints

* 1<=n<=10^6

Output Format

Print a single base-10 integer denoting the maximum number of consecutive '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 is 101, so the maximum number of consecutive 1's is 1.

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




Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAVE A GOOD DAY 😁








 Solution:
( java )


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

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        int count=0;
        while (n!=0)
        {
            // This operation reduces length
            // of every sequence of 1s by one.
            n = (n & (n << 1));

            count++;
        }
        System.out.println(count);

        scanner.close();
    }

}


If you need solutions in other programming languages or have any doubts regarding this problem then leave a comment down below . 

Post a Comment

0 Comments

Top Post Ad

Below Post Ad