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.
String[] groups = Integer.toBinaryString(n).split("0");
Integer result = Arrays.asList(groups)
.stream()
.map(group -> group.length())
.max(Comparator.naturalOrder())
.get();
System.out.println(result);
max() is returning back an "Optional" however, there is no any test that check "0" as input value, so binary string is always having at least one "1", so it is save to call Optional.get() without checking "isPresented()".
For me looks nice.. ;) However, you are welcome with improvements suggestions.
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 →
Java 8 approach:
For me looks nice.. ;) However, you are welcome with improvements suggestions.