Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created February 4, 2016 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/9c1e9dfe70b339dcba7b to your computer and use it in GitHub Desktop.
Save adamloving/9c1e9dfe70b339dcba7b to your computer and use it in GitHub Desktop.
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