Created
June 30, 2018 11:05
-
-
Save desigens/6d1cca7d7d1b102bbaa29d37850f91e7 to your computer and use it in GitHub Desktop.
Codility BinaryGap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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