w3resource

Java: Find the length of the longest sequence of zeros in binary representation of an integer


Longest Binary Zero Sequence

Write a Java program to find the length of the longest sequence of zeros in binary representation of an integer.

Sample example:
Number 7 has binary representation 111 and has no binary gaps.
Number 8 has binary representation 1000 and contains a binary gap of length 0.
Number 457 has binary representation 111001001 and contains a binary gap of length 2.
Number 40 has binary representation 101000 and contains one binary gap of length 1.
Number 12546 has binary representation 11000100000010 and contains highest binary gap of length 6.

Sample Solution:

Java Code:

import java.util.*;
 public class Example13 {
     public static void main(String[] args){
		 
		int dec_num, rem, quot, i=1, j;  
        int bin_num[] = new int[100];  
        Scanner scan = new Scanner(System.in);  
          
        System.out.print("Input a Decimal Number : ");  
        dec_num = scan.nextInt();  
          
        quot = dec_num;  
          
        while(quot != 0)  
        {  
            bin_num[i++] = quot%2;  
            quot = quot/2;  
        }  
		String binary_str="";
		System.out.print("Binary number is: ");  
        for(j=i-1; j>0; j--)  
        {  
          binary_str = binary_str + bin_num[j];	
        }  
		System.out.print(binary_str);
        i = binary_str.length()-1;
        while(binary_str.charAt(i)=='0') {
            i--;
        }
        int length = 0;
        int ctr = 0;
        for(; i>=0; i--) {
            if(binary_str.charAt(i)=='1') {
                length = Math.max(length, ctr);
                ctr = 0;
            } else {
                ctr++;
            }
        }
        length = Math.max(length, ctr);
        System.out.println("\nLength of the longest sequence: "+length);
    }
 }

Sample Output:

Input a Decimal Number : 7                                             
Binary number is: 111                                                  
Length of the longest sequence: 0   

Flowchart:

Flowchart: Find the length of the longest sequence of zeros in binary representation of an integer.


For more Practice: Solve these Related Problems:

  • Write a Java program to find the longest sequence of consecutive zeros in the binary representation of an integer using bitwise operations.
  • Write a Java program to convert an integer to binary and then compute the length of the longest zero-run via iteration.
  • Write a Java program to recursively determine the longest sequence of zeros in an integer's binary representation.
  • Write a Java program to implement a method that uses string processing to find the longest run of zeros in the binary form of a number.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to count the number of prime numbers less than a given positive number.
Next: Write a Java program to find the square root of a number using Babylonian method.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.