Given a number n, find length of the longest consecutive 1s in its binary representation.
Input : n = 14 Output : 3 The binary representation of 14 is 1110. Input : n = 222 Output : 4 The binary representation of 222 is 11011110.
We have existing solution for this problem please refer Length of the Longest Consecutive 1s in Binary Representation link. We can solve this problem quickly in python. Approach is very simple,
- Convert decimal number into it’s binary using bin() function and remove starting first two characters ‘0b’ because bin() function returns binary representattion of number in string form and appends ‘0b’ as prefix.
- Separate all sub-strings of consecutive 1’s separated by zeros using split() method of string.
- Print maximum length of splitted sub-strings of 1’s.
- Python - Maximum of Consecutive Pair in integer list
- Python | Check if binary representation is palindrome
- Python | Find longest consecutive letter and digit substring
- Python - Ways to print longest consecutive list without considering duplicates element
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Python | Average of each n-length consecutive segment in a list
- Python - Maximum length consecutive positive elements
- Python - K length consecutive characters
- Binary string with given frequencies of sums of consecutive pairs of characters
- Python | Extract length of longest string in list
- Python - Longest Substring Length of K
- Python | Check if there are K consecutive 1's in a binary number
- Python Program to Count number of binary strings without consecutive 1's
- Python | Binary list to integer
- Python - Convert Binary tuple to Integer
- Python Program for Zeckendorf\'s Theorem (Non-Neighbouring Fibonacci Representation)
- Python | Convert a string representation of list into list
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- How to create a COVID19 Data Representation GUI?
- Python program to check if the list contains three consecutive common numbers in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.