Created
December 30, 2015 16:43
-
-
Save markdrake/51bd04b973337cb60aa4 to your computer and use it in GitHub Desktop.
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 binaryGapFnc(N) { | |
var binary = N.toString(2); | |
var regex = /10+1/g | |
var matches = binary.match(regex); | |
if(!matches) { return 0; } | |
var binaryGap = 0; | |
for(var i = 0; i < matches.length; i++) { | |
if (matches[i].length > binaryGap) { | |
binaryGap = matches[i].length - 2; // We remove the two 1's | |
} | |
} | |
return binaryGap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment