Created
March 7, 2020 06:57
-
-
Save Lelith/ed6403c91662bfd48372cc1ca67c2acb to your computer and use it in GitHub Desktop.
binary gap solution
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) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let binaryGaps = (N).toString(2); // returns binary string | |
binaryGaps = binaryGaps.replace(/^0*/,''); // remove leading zeros (no gap) | |
binaryGaps = binaryGaps.replace(/(0*$)/, ''); // remove trailing zeros (no gap) | |
binaryGaps = binaryGaps.split(1); // split on 1 to get gaps | |
const longestGap = Math.max(...(binaryGaps.map(gap => gap.length))); // get maximum number | |
return longestGap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment