PROGRAM TO COUNT CONSECUTIVE BINARY ONES IN A STRING
PROGRAM TO COUNT CONSECUTIVE BINARY ONES IN A STRING
Input 1
5
Output 1
1
Input 2
13
Output 2
2
SOLUTION:
package Numbers;
import java.util.*;
public class Countofconsecutive1sinBinary {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
char[] binary = Integer.toBinaryString(n).toCharArray();
int tmpCount = 0;
int maxCount = 0;
for(int i = 0; i < binary.length; i++){
tmpCount = (binary[i] == '0') ? 0 : tmpCount + 1;
if(tmpCount > maxCount){
maxCount = tmpCount;
}
}
System.out.println(maxCount);
}
}
Comments
Post a Comment