Skip to content

Instantly share code, notes, and snippets.

@amorfati0310
Created March 21, 2018 05:06
Show Gist options
  • Save amorfati0310/e790d9b6b717096ae5e7166c939ff5fb to your computer and use it in GitHub Desktop.
Save amorfati0310/e790d9b6b717096ae5e7166c939ff5fb to your computer and use it in GitHub Desktop.
codility_binary_01
function solution(number) {
// write your code in JavaScript (Node.js 8.9.4)
let before,
rest,
zeroGapStart = false,
zeroCount = 0,
maxZeros = 0;
const Two = 2;
while(number > 0){
rest = number%Two;
if(before^rest){
if(rest){
zeroGapStart = false;
maxZeros = Math.max(zeroCount, maxZeros);
zeroCount = 0;
}else{
zeroGapStart = true;
zeroCount +=1;
}
}
if(!before & !rest){
if(zeroGapStart){
zeroCount +=1;
}
}
before = rest;
number = parseInt(number/2);
}
return maxZeros;
}
console.log(solution(328));
//n=328=101001000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment