# Find longest sequence of 1’s in binary representation with one flip

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.

**Examples:**

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 .

**result = return max-length+1**(// add 1 for flip bit count )

.

Below is the implementation of above idea :

## C++

`// C++ program to find maximum consecutive ` `// 1's in binary representation of a number ` `// after flipping one bit. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `flipBit(unsigned a) ` `{ ` ` ` `/* If all bits are l, binary representation ` ` ` `of 'a' has all 1s */` ` ` `if` `(~a == 0) ` ` ` `return` `8*` `sizeof` `(` `int` `); ` ` ` ` ` `int` `currLen = 0, prevLen = 0, maxLen = 0; ` ` ` `while` `(a!= 0) ` ` ` `{ ` ` ` `// If Current bit is a 1 then increment currLen++ ` ` ` `if` `((a & 1) == 1) ` ` ` `currLen++; ` ` ` ` ` `// If Current bit is a 0 then check next bit of a ` ` ` `else` `if` `((a & 1) == 0) ` ` ` `{ ` ` ` `/* Update prevLen to 0 (if next bit is 0) ` ` ` `or currLen (if next bit is 1). */` ` ` `prevLen = (a & 2) == 0? 0 : currLen; ` ` ` ` ` `// If two consecutively bits are 0 ` ` ` `// then currLen also will be 0. ` ` ` `currLen = 0; ` ` ` `} ` ` ` ` ` `// Update maxLen if required ` ` ` `maxLen = max(prevLen + currLen, maxLen); ` ` ` ` ` `// Remove last bit (Right shift) ` ` ` `a >>= 1; ` ` ` `} ` ` ` ` ` `// We can always have a sequence of ` ` ` `// at least one 1, this is fliped bit ` ` ` `return` `maxLen+1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// input 1 ` ` ` `cout << flipBit(13); ` ` ` `cout << endl; ` ` ` ` ` `// input 2 ` ` ` `cout << flipBit(1775); ` ` ` `cout << endl; ` ` ` ` ` `// input 3 ` ` ` `cout << flipBit(15); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum consecutive ` `// 1's in binary representation of a number ` `// after flipping one bit. ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `flipBit(` `int` `a) ` ` ` `{ ` ` ` `/* If all bits are l, binary representation ` ` ` `of 'a' has all 1s */` ` ` `if` `(~a == ` `0` `) ` ` ` `{ ` ` ` `return` `8` `* sizeof(); ` ` ` `} ` ` ` ` ` `int` `currLen = ` `0` `, prevLen = ` `0` `, maxLen = ` `0` `; ` ` ` `while` `(a != ` `0` `) ` ` ` `{ ` ` ` `// If Current bit is a 1 ` ` ` `// then increment currLen++ ` ` ` `if` `((a & ` `1` `) == ` `1` `) ` ` ` `{ ` ` ` `currLen++; ` ` ` `} ` ` ` ` ` `// If Current bit is a 0 then ` ` ` `// check next bit of a ` ` ` `else` `if` `((a & ` `1` `) == ` `0` `) ` ` ` `{ ` ` ` `/* Update prevLen to 0 (if next bit is 0) ` ` ` `or currLen (if next bit is 1). */` ` ` `prevLen = (a & ` `2` `) == ` `0` `? ` `0` `: currLen; ` ` ` ` ` `// If two consecutively bits are 0 ` ` ` `// then currLen also will be 0. ` ` ` `currLen = ` `0` `; ` ` ` `} ` ` ` ` ` `// Update maxLen if required ` ` ` `maxLen = Math.max(prevLen + currLen, maxLen); ` ` ` ` ` `// Remove last bit (Right shift) ` ` ` `a >>= ` `1` `; ` ` ` `} ` ` ` ` ` `// We can always have a sequence of ` ` ` `// at least one 1, this is fliped bit ` ` ` `return` `maxLen + ` `1` `; ` ` ` `} ` ` ` ` ` `static` `byte` `sizeof() ` ` ` `{ ` ` ` `byte` `sizeOfInteger = ` `8` `; ` ` ` `return` `sizeOfInteger; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// input 1 ` ` ` `System.out.println(flipBit(` `13` `)); ` ` ` ` ` `// input 2 ` ` ` `System.out.println(flipBit(` `1775` `)); ` ` ` ` ` `// input 3 ` ` ` `System.out.println(flipBit(` `15` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum ` `# consecutive 1's in binary ` `# representation of a number ` `# after flipping one bit. ` `def` `flipBit(a): ` ` ` ` ` `# If all bits are l, ` ` ` `# binary representation ` ` ` `# of 'a' has all 1s ` ` ` `if` `(~a ` `=` `=` `0` `): ` ` ` `return` `8` `*` `sizeof(); ` ` ` ` ` `currLen ` `=` `0` `; ` ` ` `prevLen ` `=` `0` `; ` ` ` `maxLen ` `=` `0` `; ` ` ` `while` `(a > ` `0` `): ` ` ` ` ` `# If Current bit is a 1 ` ` ` `# then increment currLen++ ` ` ` `if` `((a & ` `1` `) ` `=` `=` `1` `): ` ` ` `currLen ` `+` `=` `1` `; ` ` ` ` ` `# If Current bit is a 0 ` ` ` `# then check next bit of a ` ` ` `elif` `((a & ` `1` `) ` `=` `=` `0` `): ` ` ` ` ` `# Update prevLen to 0 ` ` ` `# (if next bit is 0) ` ` ` `# or currLen (if next ` ` ` `# bit is 1). */ ` ` ` `prevLen ` `=` `0` `if` `((a & ` `2` `) ` `=` `=` `0` `) ` `else` `currLen; ` ` ` ` ` `# If two consecutively bits ` ` ` `# are 0 then currLen also ` ` ` `# will be 0. ` ` ` `currLen ` `=` `0` `; ` ` ` ` ` `# Update maxLen if required ` ` ` `maxLen ` `=` `max` `(prevLen ` `+` `currLen, maxLen); ` ` ` ` ` `# Remove last bit (Right shift) ` ` ` `a >>` `=` `1` `; ` ` ` ` ` `# We can always have a sequence ` ` ` `# of at least one 1, this is ` ` ` `# fliped bit ` ` ` `return` `maxLen ` `+` `1` `; ` ` ` `# Driver code ` `# input 1 ` `print` `(flipBit(` `13` `)); ` ` ` `# input 2 ` `print` `(flipBit(` `1775` `)); ` ` ` `# input 3 ` `print` `(flipBit(` `15` `)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum consecutive ` `// 1's in binary representation of a number ` `// after flipping one bit. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `flipBit(` `int` `a) ` ` ` `{ ` ` ` `/* If all bits are l, binary representation ` ` ` `of 'a' has all 1s */` ` ` `if` `(~a == 0) ` ` ` `{ ` ` ` `return` `8 * ` `sizeof` `(` `int` `); ` ` ` `} ` ` ` ` ` `int` `currLen = 0, prevLen = 0, maxLen = 0; ` ` ` `while` `(a != 0) ` ` ` `{ ` ` ` `// If Current bit is a 1 ` ` ` `// then increment currLen++ ` ` ` `if` `((a & 1) == 1) ` ` ` `{ ` ` ` `currLen++; ` ` ` `} ` ` ` ` ` `// If Current bit is a 0 then ` ` ` `// check next bit of a ` ` ` `else` `if` `((a & 1) == 0) ` ` ` `{ ` ` ` `/* Update prevLen to 0 (if next bit is 0) ` ` ` `or currLen (if next bit is 1). */` ` ` `prevLen = (a & 2) == 0 ? 0 : currLen; ` ` ` ` ` `// If two consecutively bits are 0 ` ` ` `// then currLen also will be 0. ` ` ` `currLen = 0; ` ` ` `} ` ` ` ` ` `// Update maxLen if required ` ` ` `maxLen = Math.Max(prevLen + currLen, maxLen); ` ` ` ` ` `// Remove last bit (Right shift) ` ` ` `a >>= 1; ` ` ` `} ` ` ` ` ` `// We can always have a sequence of ` ` ` `// at least one 1, this is fliped bit ` ` ` `return` `maxLen + 1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `// input 1 ` ` ` `Console.WriteLine(flipBit(13)); ` ` ` ` ` `// input 2 ` ` ` `Console.WriteLine(flipBit(1775)); ` ` ` ` ` `// input 3 ` ` ` `Console.WriteLine(flipBit(15)); ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find maximum consecutive ` `// 1's in binary representation of a number ` `// after flipping one bit. ` ` ` `function` `flipBit(` `$a` `) ` `{ ` ` ` `/* If all bits are l, ` ` ` `binary representation ` ` ` `of 'a' has all 1s */` ` ` `if` `(~` `$a` `== 0) ` ` ` `return` `8 * sizeof(); ` ` ` ` ` `$currLen` `= 0; ` ` ` `$prevLen` `= 0; ` ` ` `$maxLen` `= 0; ` ` ` `while` `(` `$a` `!= 0) ` ` ` `{ ` ` ` ` ` `// If Current bit is a 1 ` ` ` `// then increment currLen++ ` ` ` `if` `((` `$a` `& 1) == 1) ` ` ` `$currLen` `++; ` ` ` ` ` `// If Current bit is a 0 ` ` ` `// then check next bit of a ` ` ` `else` `if` `((` `$a` `& 1) == 0) ` ` ` `{ ` ` ` ` ` `/* Update prevLen to 0 ` ` ` `(if next bit is 0) ` ` ` `or currLen (if next ` ` ` `bit is 1). */` ` ` `$prevLen` `= (` `$a` `& 2) == 0? 0 : ` `$currLen` `; ` ` ` ` ` `// If two consecutively bits are 0 ` ` ` `// then currLen also will be 0. ` ` ` `$currLen` `= 0; ` ` ` `} ` ` ` ` ` `// Update maxLen if required ` ` ` `$maxLen` `= max(` `$prevLen` `+ ` `$currLen` `, ` `$maxLen` `); ` ` ` ` ` `// Remove last bit (Right shift) ` ` ` `$a` `>>= 1; ` ` ` `} ` ` ` ` ` `// We can always have a sequence of ` ` ` `// at least one 1, this is fliped bit ` ` ` `return` `$maxLen` `+1; ` `} ` ` ` ` ` `// Driver code ` ` ` `// input 1 ` ` ` `echo` `flipBit(13); ` ` ` `echo` `"\n"` `; ` ` ` ` ` `// input 2 ` ` ` `echo` `flipBit(1775); ` ` ` `echo` `"\n"` `; ` ` ` ` ` `// input 3 ` ` ` `echo` `flipBit(15); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

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 contribute@geeksforgeeks.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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- 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
- Find value of k-th bit in binary representation
- Find the n-th number whose binary representation is a palindrome
- 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 consecutive 1s of length >= n in binary representation of a number
- Horizontally Flip a Binary Matrix
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Longest sub-sequence with a given OR value : O(N) Approach
- Binary representation of next number
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Binary representation of a given number
- Largest number with binary representation is m 1's and m-1 0's
- Next greater number than N with exactly one bit different in binary representation of N
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Binary representation of previous number