Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Created August 11, 2019 15:05
Show Gist options
  • Save jmlavoier/4f564bab93ddf08d9270ffaa7405d120 to your computer and use it in GitHub Desktop.
Save jmlavoier/4f564bab93ddf08d9270ffaa7405d120 to your computer and use it in GitHub Desktop.
codility - binary gap
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