Give an integer n. We can flip exactly one bit. Write code to find the length of the longest sequence of 1 s you could create.
Input : 1775 Output : 8 Binary representation of 1775 is 11011101111. After flipping the highlighted bit, we get consecutive 8 bits. 11011111111. Input : 12 Output : 3 Input : 15 Output : 5 Input : 71 Output: 4 Binary representation of 71 is 1000111. After flipping the highlighted bit, we get consecutive 4 bits. 1001111.
A simple solution is to store binary representation of given number in a binary array. Once we have elements in binary array, we can apply methods discussed here.
An efficient solution is to walk through the bits in binary representation of given number. We keep track of current 1’s sequence length and the previous 1’s sequence length. When we see a zero, update previous Length:
- If the next bit is a 1, previous Length should be set to current Length.
- If the next bit is a 0, then we can’t merge these sequences together. So, set previous Length to 0.
We update max length by comparing following two:
- Current value of max-length
- Current-Length + Previous-Length .
Below is the implementation of above idea :
4 8 5
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Next greater number than N with exactly one bit different in binary representation of N
- Length of the Longest Consecutive 1s in Binary Representation
- Longest common substring in binary representation of two numbers
- Length of longest consecutive zeroes in the binary representation of a number.
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Horizontally Flip a Binary Matrix
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Length of second longest sequence of consecutive 1s in a binary array
- Find value of k-th bit in binary representation
- Find consecutive 1s of length >= n in binary representation of a number
- Find next greater element with no consecutive 1 in it's binary representation
- Find the number obtained after concatenation of binary representation of M and N
- Find the n-th number whose binary representation is a palindrome
- Count minimum bits to flip such that XOR of A and B equal to C
- Binary representation of a given number
- Check if binary representation of a number is palindrome
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Decimal representation of given binary string is divisible by 5 or not
- Binary representation of next number