Skip to content

Instantly share code, notes, and snippets.

@olegkalyta
Created December 24, 2016 19:03
Show Gist options
  • Save olegkalyta/bfa22027a1e8594ce04a3379dbc01a7c to your computer and use it in GitHub Desktop.
Save olegkalyta/bfa22027a1e8594ce04a3379dbc01a7c to your computer and use it in GitHub Desktop.
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