BinaryGap

by me, Javascript

Problem: 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. Write an efficient algorithm that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

Solution:

First i take the number and turn it to a binary string.

let binary = (N >>> 0).toString(2)

Then I want turn the string into an array by splitting it using 1 as an identifier so I get the strings of zeros or empty strings.

let zeros = binary.split('1')

Let's set up the length variable that we will update with the length of the longest string of zeros and create the loop to check each item in the zeros array.

let longest = 0
zeros.forEach((item, index) => {})

I then created a condition that would make sure the index was less than the length of the zeros array minus 1. I did this because the last item of the array is either an empty array or it wasn't a string of zeros surrounded by a 1. I could have also achieved this by just popping off the last item of the array before looping through them.

Then, I want to see if the length of the string is longer than longest and if it is, set that length to longest. Once the loop is completed longest should represent the length of the longest sequence of consecutive zeros in the binary of a number.

zeros.forEach((item, index) => {
  if (index < zeros.length - 1)
    longest = item.length > longest ? item.length : longest
})

The final function looked like this. This solution got an 100% Total Score.

function solution(N) {
  let binary = (N >>> 0).toString(2)
  let zeros = binary.split('1')
  let longest = 0

  zeros.forEach((item, index) => {
    if (index < zeros.length - 1)
      longest = item.length > longest ? item.length : longest
  })

  return longest
}

I'm sure there are many other solutions but this one got the job done!!

On to the next.

More articles

MissingInteger

Find the smallest positive integer that does not occur in a given sequence.

Read more

OddOccurrencesInArray

Find value that occurs in odd number of elements.

Read more

Let’s work together!!

You can find me in person at:

  • Edmonton
    Wherever there’s good internet
    Alberta, Canada