Created
April 16, 2020 06:05
-
-
Save lumentut/cab50ccd1ce0c230a250b1f8bf5a19e4 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) { | |
// Convert number to a binary string | |
const binString = (N >>>0).toString(2) | |
// Remove trailing zeros from the string | |
while(binString.slice(-1) === "0") { | |
binString = binString.slice(0,-1) | |
} | |
// Get gaps array by using split(1) then remove "" from the array | |
const gaps = binString.split(1).filter(e => e !== "") | |
// Return 0 if no binary gap found | |
if(gaps.length == 0) { | |
return 0 | |
} | |
// Get the maximum length of gaps in gaps array | |
const lengthestGap = gaps.sort((x,y) => y.length - x.length)[0] | |
// Then return the length | |
return lengthestGap.length | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment