learn kro favicon img

Day 10: Binary Numbers | HackerRank Solution

Hello coders, today we are going to solve Day 10: Binary Number HackerRank Solution in C++Java and Python.

Objective

Today, we’re working with binary numbers.

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

Sample Output 1

Sample Input 2

Sample Output 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.

Solution – Day 10: Binary Numbers

C++

Java

Python

Disclaimer: The above Problem (Day 10: Binary Numbers) is generated by Hacker Rank but the Solution is Provided by LearnKro. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *