
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Length of Longest Consecutive 1s in Binary Representation of an Integer
The length of the longest consecutive 1s in the binary representation of a given integer involves converting the integer to its binary form and then identifying the longest run of contiguous 1s.
Here, we can represent each integer in binary as a series of 0s and 1s. Bitwise operations allow efficient bit manipulation, making it easy to count and update the longest sequence of consecutive 1s.
Consider special cases such as when the integer is 0 (binary representation 0) or when it contains only 1s (e.g., for the number 7, binary 111).
To understand the concept clearly, Let's go through the example?
Number = 46 Binary representation = 101110
The length of the longest consecutive 1's in binary representation of 46 = 3.
To find the length of the Longest Consecutive 1's in Binary Representation of a given integer in Java is quite easy. Let us learn the following approaches:
Using Bitwise Approach
This approach uses bitwise operations to find the longest sequence of consecutive 1s in the Binary Representation of a given integer.
Example
This program counts the longest sequence of consecutive 1s in the binary representation of a given integer. It iterates through each bit of the number, counting consecutive 1s and updates the maximum count found.
public class LongestConsecutiveOnes { public static void main(String[] args) { int num = 46; System.out.println("Longest consecutive 1s= " + longestConsecutiveOnes(num)); } public static int longestConsecutiveOnes(int num) { int maxCount = 0; int count = 0; while (num != 0) { if ((num & 1) == 1) { count++; maxCount = Math.max(maxCount, count); } else { count = 0; } num >>= 1; } return maxCount; } }
Output
The above program produce the following result ?
Longest consecutive 1s= 3
Using String Conversion Approach
This is another approach to convert the integer to a binary string and then find the longest sequence of consecutive 1s.
Example
In this program, we convert the given integer to its binary string representation, split the string by '0's to isolate groups of consecutive 1s and determine the length of the longest group.
public class LongestConsecutiveOnes { public static void main(String[] args) { int num = 46; System.out.println("Longest consecutive 1s= " + longestConsecutiveOnes(num)); } public static int longestConsecutiveOnes(int num) { String binaryString = Integer.toBinaryString(num); String[] onesGroups = binaryString.split("0"); int maxCount = 0; for (String group : onesGroups) { maxCount = Math.max(maxCount, group.length()); } return maxCount; } }
Output
Following is the output of the above program ?
Longest consecutive 1s= 3
Using the Regular Expression Approach
Another approach that uses regular expressions to find the longest sequence of consecutive 1s in the binary string representation.
Example
In this program, we convert the given integer to its binary string representation using a regular expression to find groups of consecutive 1s and determine the length of the longest group.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LongestConsecutiveOnes { public static void main(String[] args) { int num = 46; System.out.println("Longest consecutive 1s= " + longestConsecutiveOnes(num)); } public static int longestConsecutiveOnes(int num) { String binaryString = Integer.toBinaryString(num); Pattern pattern = Pattern.compile("1+"); Matcher matcher = pattern.matcher(binaryString); int maxCount = 0; while (matcher.find()) { maxCount = Math.max(maxCount, matcher.group().length()); } return maxCount; } }
Output
Following is the output of the above program ?
Longest consecutive 1s= 3