Created
April 16, 2020 01:25
-
-
Save Abdelhak-Bahri/f0f057886f5754ef6690e678f9011639 to your computer and use it in GitHub Desktop.
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N
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 BinaryGap(N) { | |
//get N binary representation | |
let Nbin = N.toString(2); | |
//trim from start to last 1 | |
const lastIndexOfOne = Nbin.lastIndexOf('1') | |
Nbin = Nbin.substr(0,lastIndexOfOne); | |
//split by 1 | |
let Gaps = Nbin.split('1').map(el=> el.length); | |
// take max length | |
return Math.max(...Gaps); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment