You are viewing a single comment's thread. Return to all comments →
Another implementation inspired from above solution
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); String intToBinary = Integer.toBinaryString(n); int consecutiveOne = 0; for (String str : intToBinary.split("0")) { consecutiveOne = str.length() > consecutiveOne ? str.length() : consecutiveOne; } System.out.println(consecutiveOne); }
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
Another implementation inspired from above solution
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); String intToBinary = Integer.toBinaryString(n); int consecutiveOne = 0; for (String str : intToBinary.split("0")) { consecutiveOne = str.length() > consecutiveOne ? str.length() : consecutiveOne; } System.out.println(consecutiveOne); }