Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Last active November 23, 2017 19:39
Show Gist options
  • Save abdulateef/b7a1fc61856670cd5b30a8783cbdd53d to your computer and use it in GitHub Desktop.
Save abdulateef/b7a1fc61856670cd5b30a8783cbdd53d to your computer and use it in GitHub Desktop.
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.util.Scanner;
public class Binary
{
public static String convertToBinary(int decnumber)
{
String binaryString = " ";
if (decnumber > 0)
{
binaryString = Integer.toBinaryString(decnumber);
}
return binaryString;
}
public static int findMaxChar(String str)
{
char[] array = str.toCharArray();
int maxCount = 1;
char maxChar = array[0];
for(int i = 0, j = 0; i < str.length() - 1; i = j)
{
int count = 1;
while (++j < str.length() && array[i] == array[j])
{
count++;
}
if (count > maxCount)
{
maxCount = count;
maxChar = array[i];
}
}
return (maxCount);
}
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int decimalnumber = input.nextInt();
String result = convertToBinary(decimalnumber);
System.out.println(findMaxChar(result));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment