- Related Questions & Answers
- Maximum length of segments of 0’s and 1’s in C++
- Python map function to find the row with the maximum number of 1’s
- Count Binary String without Consecutive 1's
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- 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
- Python program using map function to find row with maximum number of 1's
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Python program to check if there are K consecutive 1’s in a binary number?
- 1’s and 2’s complement of a Binary Number?
- Maximum consecutive one’s (or zeros) in a binary circular array in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Python program using the map function to find a row with the maximum number of 1's
- Python Program to Count number of binary strings without consecutive 1’
- Program to Count number of binary strings without consecutive 1’s in C/C++?

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

Sometimes when dealing with the binary representation of numbers we may be needed to find out how many continuous 1’s are present in the number. This article shows two ways how we can find out that.

The split function in python can be used to split the given string into multiple strings. We split it by zeros and the map function is used to find the maximum length among the splits generated.

data = '11110000111110000011111010101010101011111111' def Max_len_cons_1(data): print ("Maximum Number of consecutive one's: ",max(map(len,data.split('0'))) ) Max_len_cons_1(data)

Running the above code gives us the following result −

Maximum Number of consecutive one's: 8

The re module in python can also be used to count the maximum number of consecutive 1’s. Here we find the pattern of 1+ which indicates one or more number of 1’s present. Then find the maximum length among those pattern.

data = '11110000111110010011' import re the_ones = re.findall(r"1+", data) print("The blocks of one's: ",the_ones) print("Maximum Number of consecutive one's =", len(max(the_ones, key=len)))

Running the above code gives us the following result −

The blocks of one's: ['1111', '11111', '1', '11'] Maximum Number of consecutive one's = 5

Advertisements