Find the Longest Sequence of Zeros in the Binary Representation of an Integer

The problem is to transform an integer and represent it as a sequence of binary digits and then to determine the most significant sequence of zeros that one's enclose. In other words, if the binary representation string does not contain any zeros placed between ones, the result should be 0.

Furthermore, zero suffixes of a binary number should not be taken if they are not bounded by one. In binary form, the numeral 9 equals 1001. The length of the most extended sequence of zeros is 00, so the binary gap is 2. So, the number 20 in the binary form will be 10100. The most extended sequence of zeros is 0, so the binary gap is 1.

Approach-1

  • Convert the Integer to Binary: As in Java, converting an int to a binary string can be done with the help of an Integer.
  • Trim and Identify Gaps: After conversion, remove extra zeros subsequent to the ones if not enclosed by ones. Next, use the fact that the binary string can be split at every 1 and examine the size of zero sequences only.
  • Find the Longest Sequence: Traverse through the list and find the maximum length of the zero sequence.

Let's implement the above approach in a Java program.

File Name: LongestBinaryGap.java

Output:

 
The longest binary gap for 529 is: 4   

Time Complexity: Incidentally, the algorithm also has the time complexity of O(log n) because after converting the integer into binary format, the algorithm only moves through the binary string or the number of digits, which is equivalent to log n.

Space Complexity: The space complexity is O(log n), for the most part, due to storing the binary form of an integer, which will be of size approximately log n.

Approach-2

  • Skip Trailing Zeros: First of all, to avoid the correct representation of the gaps, it is essential to remove trailing zeros in the given binary representation.
  • Iterate Through Bits: Iterate through the given integer as a number and count the bits '0's between two '1's.
  • Track Maximum Gap: Store the maximum gap length for later use, but do not store the entire binary string.

Let's implement the above approach in a Java program.

File Name: LongestBinaryGap.java

Output:

 
The longest binary gap for 529 is: 4   

Time Complexity: The time complexity of the solution is log(n). Here, each bit of an integer takes some number of steps, and their number is proportional to the number of bits present in the integer.

Space Complexity: The space complexity is O(1) since the program creates only several integer Variables irrespective of the size of the input.

Conclusion

The first solution based on using string manipulation provides a conventional, obvious solution with the complexity of space O(log n), but it is still sufficient. However, the above bitwise does not require the storage of the entire binary string and has O(1) space complexity by operating on each bit only.

Both of them belong to the O(log n) time complexity, but the first method requires less amount of space, and in environments where space is a constraint, then the second method is preferable.