Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Codility binary gap solution
// 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
You can’t perform that action at this time.