Created
October 29, 2016 18:03
-
-
Save ao/a1e201375dc52feb13e2423f1255419f to your computer and use it in GitHub Desktop.
Codility: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
This file contains 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
function solution(N) { | |
var binary = N.toString(2); | |
var length = binary.length; | |
var ones = []; | |
for(var i=0; i<length; i++) { | |
if (binary[i]=='1') ones.push(i); | |
} | |
var zeros = binary.split('1'); | |
var returnVar = 0; | |
for(var i=1; i<ones.length; i++) { | |
if (zeros[i] && returnVar<zeros[i].length) returnVar = zeros[i].length; | |
} | |
return returnVar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
100%