- Related Questions & Answers
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Count Binary String without Consecutive 1's
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Count number of binary strings without consecutive 1's in C
- Program to find length of longest consecutive sequence in Python
- Python program using the map function to find a row with the maximum number of 1's
- Find the largest rectangle of 1’s with swapping of columns allowed in Python
- C++ Program to Find the Length of a String
- C program to find the length of a string?
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given the number, find length of the longest consecutive 1's in its binary representation.

Input: n = 15 Output: 4 The binary representation of 14 is 1111.

Step 1: input the number. Step 2: use one counter variable c=0. Step 3: Count the number of iterations to reach i = 0. Step 4: This operation reduces length of every sequence of 1s by one.

# Python program to find # length of the longest # consecutive 1s in # binary representation of a number. def maxlength(n): # Initialize result c = 0 # Count the number of iterations to # reach x = 0. while (n!=0): # This operation reduces length # of every sequence of 1s by one. n = (n & (n << 1)) c=c+1 return c # Driver code n=int(input("Enter The Number ::>")) print("Maximum Length of 1's ::>",maxlength(n))

Enter The Number ::>15 Maximum Length of 1's ::>4

Advertisements