Skip to content

Instantly share code, notes, and snippets.

@yussan
Last active November 11, 2018 11:48
Show Gist options
  • Save yussan/ce83202a111a251564b690233cc969e6 to your computer and use it in GitHub Desktop.
Save yussan/ce83202a111a251564b690233cc969e6 to your computer and use it in GitHub Desktop.
Find longest sequence of zeros in binary representation of an integer. Ref: https://app.codility.com/programmers/lessons/1-iterations/ . Codepen: https://codepen.io/yussan/pen/wQzqbw?editors=1011
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
const bin = Math.abs(N).toString(2)
let maxGap = 0, currentGap = 0
for(let n=0;n<bin.length;n++) {
if(bin[n]=="0"){
currentGap++
}
if(bin[n]=="1" && n !=0 ){
maxGap = Math.max(maxGap, currentGap)
currentGap = 0
}
}
return parseInt(maxGap)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment