Skip to content

Instantly share code, notes, and snippets.

@Lelith
Created March 7, 2020 06:57
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 Lelith/ed6403c91662bfd48372cc1ca67c2acb to your computer and use it in GitHub Desktop.
Save Lelith/ed6403c91662bfd48372cc1ca67c2acb to your computer and use it in GitHub Desktop.
binary gap solution
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