Skip to content

Instantly share code, notes, and snippets.

@desigens
Created June 30, 2018 11:05
Show Gist options
  • Save desigens/6d1cca7d7d1b102bbaa29d37850f91e7 to your computer and use it in GitHub Desktop.
Save desigens/6d1cca7d7d1b102bbaa29d37850f91e7 to your computer and use it in GitHub Desktop.
Codility BinaryGap
# https://app.codility.com/programmers/lessons/1-iterations/binary_gap/
function solution(n) {
let max = 0;
decimalToBinaryString(n).split('1').forEach((zeros, index, array) => {
if (
zeros.length > max
&& array[index - 1] !== undefined
&& array[index + 1] !== undefined
) {
max = zeros.length;
}
});
return max;
}
function decimalToBinaryString(n) {
return n.toString(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment