We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This one is more optimized solution. There is no need to compare count and result(max in my solution) for every iteration of while loop. Hope it helps.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();intmax=0,count=0;while(n>0){if(n%2==1){count++;}else{max=Math.max(count,max);count=0;}n=n>>1;// same as n=n/2}System.out.println(Math.max(count,max));}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Consecutive 1's in Binary Numbers
You are viewing a single comment's thread. Return to all comments →
This one is more optimized solution. There is no need to compare count and result(max in my solution) for every iteration of while loop. Hope it helps.