Day 10: Binary Numbers - HackerRank 30 days of code solution
Objective
Today, we're working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!
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.
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.
Input Format
A single integer, .
Constraints
Output Format
Print a single base- integer denoting 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 .
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 .
The binary representation of is , so the maximum number of consecutive 's is .
Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count=0,min=0;
while(n>0)
{
if(n%2==1)
{
count++;
if(count>=min)
{
min=count;
}
}
else
{
count=0;
}
n=n/2;
//System.out.println(n);
//System.out.println(min);
}
System.out.println(min);
}
}
Comments
Post a Comment