# Minimum flips required to keep all 1s together in a Binary string

Given a binary string str, the task is to find the minimum number of flips required to keep all 1s together in the given binary string, i.e. there must not be any 0 between 1s in the string.

Examples:

Input: str = “0011111100”
Output: 0
Explanation: We dont need to flip any bits because all the ones are grouped together and there is no zero between any two ones.

Input: str = “11100111000101”
Output: 4
Explanation: We can flip the 4th and 5th bit to make them 1 and flip 12th and 14th bit to make them 0. So the resulting string is “11111111000000” with 4 possible flips.

Approach: To solve the problem mentioned above we will implement the dynamic programming approach where we will have the following states:

• First state is dp[i] which signifies the number of flips required to make all zeroes upto the ith bit.
• Second state dp[i] which signifies the number of flips required to make the current bit 1 such that the conditions given in the question is satisfied.

So the required answer will be minimum flips for making the current bit 1 + minimum flips for making all bits after the current bit 0 for all values of i. But if all the bits in the given string is 0 then we don’t have to change anything so we can check the minimum between our answer and number of flips required to make the string with all zeroes. So we can compute the answer by iterating over all the characters in the string where,

where

dp[i] = Minimum number of flips to set current bit to 1

dp[n-1] – dp[i] = Minimum number of flips required to make all bits after i as 0

Below is the implementation of above approach:

 `# Python implementation for Minimum number of ` `# flips required in a binary string such ` `# that all the 1’s are together ` ` `  `def` `minFlip(a): ` `     `  `     ``# Length of the binary string ` `    ``n ``=` `len``(a) ` `     `  `    ``dp ``=``[[``0``, ``0``] ``for` `i ``in` `range``(n)] ` `     `  `    ``# Initial state of the dp ` `    ``# dp will be 1 if the current ` `    ``# bit is 1 and we have to flip it ` `    ``dp[``0``][``0``]``=` `int``(a[``0``]``=``=``'1'``)  ` `     `  `    ``# Initial state of the dp ` `    ``# dp will be 1 if the current ` `    ``# bit is 0 and we have to flip it ` `    ``dp[``0``][``1``]``=` `int``(a[``0``]``=``=``'0'``)  ` `     `  ` `  `    ``for` `i ``in` `range``(``1``, n): ` `         `  `         `  `        ``# dp[i] = Flips required to ` `        ``# make all previous bits zero ` `        ``# + Flip required to make current bit zero ` `        ``dp[i][``0``]``=` `dp[i``-``1``][``0``]``+``int``(a[i]``=``=``'1'``) ` `         `  `         `  `        ``# dp[i] = mimimum flips required ` `        ``# to make all previous states 0 or make ` `        ``# previous states 1 satisfying the condition  ` `        ``dp[i][``1``]``=` `min``(dp[i``-``1``])``+``int``(a[i]``=``=``'0'``) ` `         `  `     `  ` `  `    ``answer ``=` `10``*``*``18` `     `  `    ``for` `i ``in` `range``(n): ` `        ``answer ``=` `min``(answer,  ` `                     ``dp[i][``1``]``+``dp[n``-``1``][``0``]``-``dp[i][``0``]) ` `     `  `    ``# Minimum of answer and flips ` `    ``# required to make all bits 0 ` `    ``return` `min``(answer, dp[n``-``1``][``0``])  ` `     `  ` `  `# Driver code ` `s ``=` `"1100111000101"` ` `  `print``(minFlip(s)) `

Output:

```4
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.