Skip to content

Instantly share code, notes, and snippets.

@RomaSto
Created August 27, 2020 04:48
Show Gist options
  • Save RomaSto/54ab79edfb8646652c33d81fcdb01b79 to your computer and use it in GitHub Desktop.
Save RomaSto/54ab79edfb8646652c33d81fcdb01b79 to your computer and use it in GitHub Desktop.
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
if (N === parseInt(N, 10) && N >= 1 && N <= 2147483647) {
const binaryArray = N.toString(2).split('');
let res = 0;
let acc = 0;
binaryArray.forEach((el, i, arr) => {
if (el === '0' && arr[i - 1] === '1') {
acc++;
}
else if (el === '0' && acc) {
acc++;
}
else if (el === '1') {
if (acc > res) {
res = acc;
}
acc = 0;
}
});
return res;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment