Skip to content

Instantly share code, notes, and snippets.

@Abdelhak-Bahri
Created April 16, 2020 01:25
Show Gist options
  • Save Abdelhak-Bahri/f0f057886f5754ef6690e678f9011639 to your computer and use it in GitHub Desktop.
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
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