Length of the longest consecutive zeroes in the binary representation of a number in java



You are given a number (N); you need to find the length of the longest consecutive zeroes in the binary representation of the number using different approaches.

Example

Input

N = 529
The binary form of 529 is 1000010001.

Output

The longest chain of zeroes in the binary form is 0000 with a length of 4.
Hence, the answer is 4.

Approach 1: Using Division and Modulus

In this approach we use division and modulus operations to convert decimal to binary and then count the maximum zeroes in the binary number.

Steps for implementation

The following steps explain how to find the longest sequence of zeroes by processing the number directly.

Step 1: If zero is the input, return 1 since zero's binary form has zero of length 1.

Step 2: If the number is greater than zero, repeatedly divide the number by 2 and check the remainder.

Step 3: If the remainder is 1, then compare the current count with the max count and update the max count if the current count is greater than the max count.

Step 4: Increase the count if the remainder is 0.

Step 5: Print the max count after the whole number is processed.

Implementation code

Below is the Java program that follows this approach to find the longest consecutive zero sequence.

import java.util.Scanner;
public class longestzerosequence{
   static int longestzero(int n){
      if(n==0)return 1;
      int max=0,cnt=0;
      while(n!=0){
         int rem=n%2;
         n/=2;
         if(rem==1){
            max=Math.max(max,cnt);
            cnt=0;
         }else cnt++;
      }
      max=Math.max(max,cnt);
      return max;
   }
   public static void main(String[] args){
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter a number: ");
      int num=sc.nextInt();
      System.out.println("Binary representation: " + Integer.toBinaryString(num));
      System.out.println("Longest zero sequence: "+longestzero(num));
   }
}

Output

Enter a number: 20
Binary representation: 10100
Longest zero sequence: 2

Enter a number: 8
Binary representation: 1000
Longest zero sequence: 3

Time complexity ? O(log n), where n represents the input value and the number is divided by two in each step.

Approach 2: Using String

In this method, we convert the number into a binary string and then check each character in the string to count the zeroes.

Steps to implement

These steps explain how to use a string-based approach to find the longest sequence of zeroes

Step 1: The program first changes the number to its binary value of string datatype using the function Integer.toBinaryString().

Step 2: Initialize a variable for the current count and maximum count.

Step 3: Iterate through the string character by character:

Step 4: If the character is '0' increase the current count.

Step 5: If the character is '1' compare the current count with the max count and update it if required.

Step 6: Finally print the max count value.

Implementation code

Below is the Java program that follows this string-based approach to find the longest zero sequence.

import java.util.Scanner;
public class lengthzero{
   public static int zerolength(String S){
      int currcount=0,maxcount=0;
      for(int i=0;i<S.length();i++) {
         if(S.charAt(i)=='0') {
            currcount++;
         } else {
            maxcount=Math.max(maxcount,currcount);
            currcount=0;
         }
      }
      if(currcount>0) { 
         maxcount=Math.max(maxcount,currcount);
      }
      return maxcount;
   }
   public static void main(String[] args){
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter a number : ");
      int decimalNumber=sc.nextInt();
      String binaryString=Integer.toBinaryString(decimalNumber);
      System.out.println("Binary representation: "+binaryString);
      int maxLength=zerolength(binaryString);
      System.out.println("Maximum length of consecutive zeros :  "+maxLength);
   }
}

Output

Enter a number: 46
Binary representation: 101110
Maximum length of consecutive zeros: 1

Enter a number: 24
Binary representation: 11000
Maximum length of consecutive zeros: 3

Time complexity ? O(log n), where n represents the length of the binary string.

Conclusion

We have explored two approaches to print the longest consecutive zeroes in binary numbers. The first approach uses division or modulus to process the number and is very efficient. In the second approach, we convert the number into a binary string and search through the text to detect zeros. This technique is very straightforward but is a little slow compared with the first approach and also requires extra space for storing the string data.

Updated on: 2025-02-27T12:55:23+05:30

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements