Day 10: Binary Numbers Hackerrank Solution Java
For Explanation Watch Video:
Sample Input 1
5
Sample Output 1
1
Sample Input 2
13
Sample Output 2
2
Code:
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(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); int count = 0; int max = 0; while(n!=0){ if(n%2==0){ count = 0; }else{ count++; if(count>max){ max = count; } } n = n/2; } System.out.println(max); scanner.close(); }}
Comments
Post a Comment