Created
August 11, 2019 15:05
-
-
Save jmlavoier/4f564bab93ddf08d9270ffaa7405d120 to your computer and use it in GitHub Desktop.
codility - binary gap
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(N) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
//100100001001 | |
const binaryValue = N.toString(2); | |
let start = false; | |
let count = 0; | |
let greater = 0; | |
for (let i in binaryValue) { | |
let bit = binaryValue[i]; | |
if (bit === '1') { | |
if (start) { | |
greater = greater < count ? count : greater; | |
count = 0; | |
} else { | |
start = true; | |
} | |
} else { | |
if (start) { | |
count = count + 1; | |
} | |
} | |
} | |
return greater; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment