w3resource

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

Java Math Exercises: Exercise-13 with Solution

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.

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?



Java: Tips of the Day

How to convert comma-separated String to List?

Convert comma separated String to List:

List<String> items = Arrays.asList(str.split("\\s*,\\s*"));

The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.

Please note that this returns simply a wrapper on an array: you CANNOT for example .remove() from the resulting List. For an actual ArrayList you must further use new ArrayList<String>.

Ref: https://bit.ly/30L0FLH