Java Day 10: Binary Numbers
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.
- 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 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- in.close();
-
- int sum = 0, max = 0;
- while (n > 0) {
- if (n % 2 == 1) {
- sum++;
- if (sum > max) {
- max = sum;
- }
- } else {
- sum = 0;
- }
- n = n / 2;
- }
- System.out.println(max);
- }
- }