Created
February 4, 2016 15:50
-
-
Save adamloving/9c1e9dfe70b339dcba7b to your computer and use it in GitHub Desktop.
Codility binary gap solution
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
// wrong for trailing 0s, ... | |
// 6, 328, n=16=2**4 and n=1024=2**10 | |
// n=51712=110010100000000_2 and n=20=10100_2 | |
function solution(N) { | |
var max = 0; | |
var currentLength = 0; | |
while (N > 0) { | |
var remainder = N % 2; | |
inGap = remainder === 0; | |
if (inGap) { | |
currentLength += 1; | |
max = Math.max(max, currentLength); | |
} else { | |
currentLength = 0; | |
} | |
N = Math.floor(N / 2); | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment