Binary gap - codility
by Ayan Dey
JavaScript
function solution(N) {
let curIndex = 0;
let lastIndex = -1;
let gap = 0;
let maxGap = 0;
while(N > 0) {
if(N % 2 === 1) {
if(lastIndex > -1) {
gap = curIndex - lastIndex - 1;
}
lastIndex = curIndex;
maxGap = Math.max(maxGap, gap);
}
N = Math.floor(N / 2);
curIndex++;
}
return maxGap;
}
document.write(solution(1041));