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.
public static void main(String[] args) {
int result = 0;
Scanner scanner = new Scanner(System.in);
Matcher matcher = Pattern.compile("(1+)")
.matcher(Integer.toBinaryString(scanner.nextInt()));
while (matcher.find()) {
result = Math.max(matcher.group().length(), result);
}
System.out.println(result);
}
}
** need to add two imports:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Cookie support is required to access HackerRank
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 →
Or (Java) regex based solution:
public class Solution {
}
** need to add two imports: import java.util.regex.Matcher; import java.util.regex.Pattern;