# Binary numbers

What is a binary number? For starters, it is a base-2 number system that uses only two numbers: mainly 0 and 1. The modern system was studied in the 16th and 17th centuries in Europe, but it first appeared much earlier starting in Egypt, China, and India.

Binary numbers have many useful applications such as the earliest programming language, circuitry, and images. However, we are not going to discuss these topics further. We will be discussing an algorithm question regarding binary numbers.

The question is find the longest binary gap length of zero’s in between two one’s. A number like 529 with its binary representation of 1000010001 will have a length of 4; 32 should be given 0 because its binary representation is 100000.

So how do we start this problem? First off, the number N is a standard integer so we want to convert N into a binary number. The code is

`(N >>> 0).toString(2). `

So if I were let N = 32, the binary representation is 100000. Next thing to do is split the binary number apart at 1 and if the final number in the binary is not zero, we want to remove the last element of the array

`let arr = binary.split('1')if(binary[binary.length - 1] !== '1'){    arr.pop()}. `

For example, we will use N = 32; its binary representation is 100000 so the array format is

`arr = ['', '00000']. `

Because the final number is not 1, we will remove the final element, which will leave behind

`arr ['']. `

Now the last thing to do is call a new array dubbed “newarr” and insert the length of each element into the new array like so

`for(var x = 0; x < arr.length; x++){     newarr.push(arr[x].length)}return newarr. `

Full code is

`function solution(N) {    let binary = (N >>> 0).toString(2)    let arr = binary.split('1')    let newarr = []    if(binary[binary.length - 1] !== '1'){        arr.pop()    }    for(var x = 0; x < arr.length; x++){        newarr.push(arr[x].length)    }    return Math.max(...newarr)}`

The question is from Codility, and it is completely correct for all of its cases. If there is a better way to solve this, let me know.