Last active
November 14, 2017 05:32
-
-
Save kenduigraha/81dfbd52ac0efbad74177856ef21fa69 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
var result = []; | |
var count = 0; | |
var resultCount = []; | |
function longestBinaryGap(decimal) { | |
var divide = decimal / 2; | |
var divideRound = Math.floor(divide); | |
if (decimal % 2 != 0) { | |
count = 0; | |
result.push(1) | |
} else { | |
count++; | |
result.push(0) | |
} | |
if (divideRound > 0) { | |
resultCount.push(count) | |
longestBinaryGap(divideRound); | |
} | |
return resultCount.reduce(function(a,b) { | |
return Math.max(a,b) | |
}); | |
} | |
console.log(longestBinaryGap(1041)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment