- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.

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

## Example

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

## Algorithm

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.

## Example code

# 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))

## Output

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

- Related Articles
- 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++
- Program to find length of longest consecutive path of a binary tree in python
- Count number of binary strings without consecutive 1's in C
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Python program to check if there are K consecutive 1’s in a binary number?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to find concatenation of consecutive binary numbers in Python
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Program to find largest binary search subtree from a given tree in Python

Advertisements