# Minimum non-adjacent pair flips required to remove all 0s from a Binary String

In binary strings, flipping a pair of adjacent bits can easily remove a single 0 from the string. However, when we need to remove all the 0s from the binary string, we may need to flip non-adjacent pairs of bits as well. In this article, we will discuss how to determine the minimum number of non-adjacent pair flips required to remove all the 0s from a binary string.

## Algorithm

To solve this problem, we will use a simple greedy algorithm. The idea is to always choose the pair of bits that are farthest apart from each other and have at least one 0 in between them. We can then flip these two bits, effectively removing one 0 from the string. We repeat this process until all the 0s are removed.

Let us now implement this algorithm in C++.

### Example

#include <iostream>
#include <cstring>

using namespace std;

int main() {
string s;
s="100101000";
int n = s.size();

int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
cnt++;
if (i+2 < n && s[i+2] == '0') {
i += 2;
}
else {
i++;
}
}
}

cout << cnt << endl;
return 0;
}


### Output

3


## Explanation of the Code

The above code takes a binary string as input and calculates the minimum number of non-adjacent pair flips required to remove all the 0s from the string. Let us now understand the code in detail.

First, we take the binary string as input and store it in a string variable 's'. We also store the size of the string in an integer variable 'n'.

string s;
cin >> s;
int n = s.size();


Next, we initialize a variable 'cnt' to store the count of 0s in the string. We then iterate over the string using a for loop. For every 0 we encounter, we increment the count of 0s and check if the next two bits are also 0s. If they are, we flip the pair of bits by incrementing the index by 2. Otherwise, we flip only the adjacent pair of bits by incrementing the index by 1.

int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
cnt++;
if (i+2 < n && s[i+2] == '0') {
i += 2;
}
else {
i++;
}
}
}


Finally, we output the count of non-adjacent pair flips required to remove all the 0s from the string.

cout << cnt << endl;


## Testcase Example

Let us consider the binary string "100101000". The minimum number of non-adjacent pair flips required to remove all the 0s from this string can be calculated using the above algorithm.

First, we encounter a 0 at position 2. We flip the pair (1,3) to get the string "110101000". We then encounter the next 0 at position 5. We flip the pair (1,7) to get the string "111101000". We then encounter the next 0 at position 8. We flip the pair (1,9) to get the string "111111000". All the 0s have been removed from the string now.

The count of non-adjacent pair flips required to remove all the 0s from the string is 3. We can verify this by running the above C++ code for the input string "100101000".

## Conclusion

In this article, we discussed how to determine the minimum number of non-adjacent pair flips required to remove all the 0s from a binary string. We used a simple greedy algorithm to solve this problem, and implemented it in C++ code. We also provided an example test case to illustrate the working of the algorithm.

Updated on: 18-May-2023

93 Views 