Created
March 21, 2018 05:06
-
-
Save amorfati0310/e790d9b6b717096ae5e7166c939ff5fb to your computer and use it in GitHub Desktop.
codility_binary_01
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
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