Skip to content

Instantly share code, notes, and snippets.

@tejuafonja
Created October 13, 2017 16:57
Show Gist options
  • Save tejuafonja/37e6915aeeb0d7943df7aad82bf78d5d to your computer and use it in GitHub Desktop.
Save tejuafonja/37e6915aeeb0d7943df7aad82bf78d5d to your computer and use it in GitHub Desktop.
find the max 0's in-between 1's in a binary number
function solution(N) {
var maxGap = 0;
var tempGap = 0;
var count = 0;
var binGap = N.toString(2);
var lastIndex = binGap.length - 1;
for (lastIndex; lastIndex>=0; lastIndex--) {
if (binGap[lastIndex] !== '0') {
break;
}
}
for (var i=lastIndex-1; i>=0; i--) {
if (binGap[i] === '0') {
count += 1
tempGap = count
} else {
if (tempGap > maxGap) {
maxGap = tempGap
}
count = 0
}
}
return maxGap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment