Day 10: Binary Numbers In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 10: Binary Numbers in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 10: Binary Numbers – Hacker Rank Solution

Day 10: Binary Numbers – Hacker Rank Solution

Problem:

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

Task
Given a base- integer, , convert it to binary (base-). Then find and print the base- integer denoting the maximum number of consecutive ‘s in ‘s binary representation. When working with different bases, it is common to show the base as a subscript.

Example

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

Input Format

A single integer, .

Constraints

Output Format

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

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Explanation

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

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

Day 10: Binary Numbers – Hacker Rank Solution
import java.util.Scanner;

/**
 * @author Techno-RJ
 *
 */
public class Day10BinaryNumbers {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		in.close();
		int r = n, counter = 0, maxOne = 0;
		String s = "";
		while (n > 0) {

			r = n % 2;
			if (r == 1) {
				counter++;
				if (counter > maxOne) {
					maxOne = counter;
				}
			} else {
				counter = 0;
			}
			s = r + s;
			n = n / 2;

		}
		System.out.println(maxOne);

	}
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.