Created
October 13, 2017 16:57
-
-
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
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) { | |
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