Last active
September 7, 2017 18:05
-
-
Save adyngom/bd239df7785d22286da51cf880d3860d 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 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