Binary Gap

  • May 9, 2022

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.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Given a positive integer N, find and return the longest distance between two consecutive 1’s in the binary representation of N.

If there aren’t two consecutive 1’s, return 0.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation ‘100000’ and thus no binary gaps.

Let’s write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..2,147,483,647].

1
2
3
4
5
6
7
8
9
    Input: 22
    Output: 2
    Explanation: 
    22 in binary is 0b10110.
    In the binary representation of 22, there are three ones, and 
        two consecutive pairs of 1's.
    The first consecutive pair of 1's have distance 2.
    The second consecutive pair of 1's have distance 1.
    The answer is the largest of these two distances, which is 2.

1
2
3
4
    Input: 5
    Output: 2
    Explanation: 
    5 in binary is 0b101

1
2
3
4
    Input: 6
    Output: 1
    Explanation: 
    6 in binary is 0b110

1
2
3
4
5
6
    Input: 8
    Output: 0
    Explanation: 
    8 in binary is 0b1000.
    There aren't any consecutive pairs of 1's in the binary 
        representation of 8, so we return 0

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class BinaryGapSolution {
        companion object {
            @JvmStatic
            fun main(args: Array<String>) {
                println("529: "+ solution(529)) // 4
                println("1041: "+ solution(1041)) // 5
                println("32: "+ solution(32)) // 0
                println("15: "+ solution(15)) // 0
                println("20: "+ solution(20)) // 1
    
            }
    
            fun solution(N: Int): Int {
                val binaryString = Integer.toBinaryString(N)
                println("binary \${binaryString}")
                val binaryArray = binaryString.toCharArray()
                println("binary array \${binaryArray.contentToString()}")
                var indexesOfOne = ArrayList<Int>()
                for ((index, value) in binaryArray.withIndex()) {
                    if (value == '1') {
                        indexesOfOne.add(index)
                    }
                }
                println("index array \${indexesOfOne}")
                var gap = 0
                for ((index, value) in indexesOfOne.withIndex()) {
                    if (index < indexesOfOne.size -1) {
                        val startValue = indexesOfOne.get(index)
                        val endValue = indexesOfOne.get(index+1)
                        val slicedArray = endValue - startValue - 1
                        if (slicedArray > gap) 
                            gap = slicedArray
                    }
                }
                return gap
            }
        }
    
    }
comments powered by Disqus