Skip to content

Instantly share code, notes, and snippets.

@ao
Created October 29, 2016 18:03
Show Gist options
  • Save ao/a1e201375dc52feb13e2423f1255419f to your computer and use it in GitHub Desktop.
Save ao/a1e201375dc52feb13e2423f1255419f to your computer and use it in GitHub Desktop.
Codility: 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 solution(N) {
var binary = N.toString(2);
var length = binary.length;
var ones = [];
for(var i=0; i<length; i++) {
if (binary[i]=='1') ones.push(i);
}
var zeros = binary.split('1');
var returnVar = 0;
for(var i=1; i<ones.length; i++) {
if (zeros[i] && returnVar<zeros[i].length) returnVar = zeros[i].length;
}
return returnVar;
}
@raphab3
Copy link

raphab3 commented Jun 2, 2021

100%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment