# Day 10: Binary Numbers

# Day 10: Binary Numbers

+ 111 comments check this out...

public class Solution {

`public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int rem=0,s=0,t=0; while(n>0) { rem=n%2; n=n/2; if(rem==1) { s++; if(s>=t) t=s; } else{ s=0; } } System.out.println(t); }`

}

+ 37 comments Python3: for those that don't understand the one liner print(len(max(bin(int(input().strip()))[2:].split('0'))))

for sake of explenation im going to replace input() with '13' and break the code down.

1- int(input().strip()) ==> int('13'.strip()) takes the input of the number and strips any spaces on either side, then converts it from a string to an interger. the result is the interger 13.

2- bin(13)[2:].split('0') ==> the bin() method takes a number and converts it to binary. in this case when you enter bin(13) it returns '0b1101'. the [2:] allows us to omit the '0b' at the beginning of the string. which leaves us with '1101'.split('0'). This string method takes '1101' and splits it into a list. We end up with ['11','1'].

3-len(max(['11','1'])) ==> the max() method is simply going to look for the biggest value. In this case the biggest one is '11'. '11' is passed to the len() method which just returns the length of the object in it. In this case the object is the string '11' which has two characters, so len('11') returns 2. Which in turn is also the longest consecutive amount of ones.

+ 11 comments For those using C as programming language,

int main(){ int n; scanf("%d",&n);

`int count = 0; while (n) { n = (n & (n << 1)); count++; } printf ("%d\n", count); return 0;`

}

+ 17 comments Simple way for Python3:

`print(len(max(bin(int(input().strip()))[2:].split('0'))))`

+ 3 comments Unfortunately, I don't understand the meaning of "consecutive 1s". Doesn't consecutive 1s mean occurence of a 1 right after another 1? for example, If the number is 101, shouldn't the number of consecutive 1s be 0 since there are only two ones and they are separated by a 0?

Sort 2537 Discussions, By:

Please Login in order to post a comment