Created
December 24, 2016 19:03
-
-
Save olegkalyta/bfa22027a1e8594ce04a3379dbc01a7c to your computer and use it in GitHub Desktop.
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 findMaxZerosFrom(arr, index) { | |
let zerosLength = 1 | |
let nextElement = arr[index + 1] | |
while(nextElement === '0') { | |
index++ | |
zerosLength++ | |
nextElement = arr[index + 1] | |
} | |
return zerosLength | |
} | |
function solution(N) { | |
const arr = N.toString(2).split("") | |
let searchIndex = 0 | |
const zerosLenghs = [] | |
let zerosCounts = 0 | |
while(searchIndex < arr.length) { | |
const zeroIndex = arr.indexOf('0', searchIndex) | |
if (zeroIndex > -1) { | |
const currentMaxZeros = findMaxZerosFrom(arr, zeroIndex) | |
zerosLenghs[zerosCounts] = currentMaxZeros | |
searchIndex += currentMaxZeros + 1 | |
zerosCounts++ | |
} else { | |
if (searchIndex === 0) { | |
return 0 | |
} else { | |
break | |
} | |
} | |
} | |
return Math.max(...zerosLenghs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment