Skip to content

Instantly share code, notes, and snippets.

@kenduigraha
Last active November 14, 2017 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenduigraha/81dfbd52ac0efbad74177856ef21fa69 to your computer and use it in GitHub Desktop.
Save kenduigraha/81dfbd52ac0efbad74177856ef21fa69 to your computer and use it in GitHub Desktop.
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