Skip to content

Instantly share code, notes, and snippets.

@adyngom
Last active September 7, 2017 18:05
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 adyngom/bd239df7785d22286da51cf880d3860d to your computer and use it in GitHub Desktop.
Save adyngom/bd239df7785d22286da51cf880d3860d to your computer and use it in GitHub Desktop.
function solution(N) {
const nope = 0;
if(!N) return nope;
let bin = (N >>> 0).toString(2);
let binLength = bin.length;
if (!( bin.includes("0") )) return nope;
bin = bin.split('1');
if(!bin[binLength - 1]) bin[binLength - 1] = 1;
bin = bin.filter(a => !!a);
binLength = bin.length; let maxIndex = binLength - 1;
if (!!binLength) {
let longest = 0, size;
bin.map((a, i, arr) => {
if(i !== maxIndex ) {
size = arr[i].split('').length;
if (size > longest) longest = size;
}
});
return longest;
}
};
// test it
let dec2bin = (dec) => (dec >>> 0).toString(2);
let logIt = (num) => console.log(num + " >>> " + dec2bin(num) + " - longest gap: " + solution(num));
logIt(9); // should log 2
logIt(15); // should log 0
logIt(1041); // should log 5
logIt(529); // should log 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment