# Minimum characters required to be removed to sort binary string in ascending order

In computer science, string manipulation is an essential topic that involves operations such as concatenation, substring, reversing, and more. One common problem related to string manipulation is to remove all 0s from a binary string. In this article, we will discuss an algorithm to solve this problem using a minimum number of non-adjacent pair flips.

## Problem Statement

Given a binary string, we have to remove all 0s from the string using the minimum number of non-adjacent pair flips. A flip is defined as selecting two adjacent characters and swapping them. In other words, we need to find the minimum number of flips required to make all 0s in the string to the end of the string.

## Approach

We can solve this problem using a greedy approach. We can start from the left of the string and keep track of the last index where we have flipped a 0 to the end. For each 0 we encounter, we swap it with the last flipped 0 to move it to the end of the string. If we encounter a 1, we simply move to the next index.

Let's see the algorithm in detail −

• Initialize two variables, "lastFlipped" and "flipCount" to -1 and 0, respectively.

• Traverse the binary string from left to right.

• If the current character is '0', swap it with the character at the index "lastFlipped + 1" and increment the "lastFlipped" variable.

• Increment the "flipCount" variable for every swap operation.

• Once the traversal is completed, all the 0s will be at the end of the string, and the "flipCount" will contain the minimum number of flips required to remove all 0s.

### Example

Here's the C++ code to implement the above algorithm −

#include <iostream>
#include <string>

using namespace std;

int lastFlipped = -1;
int flipCount = 0;

for (int i = 0; i < s.length(); i++) {
if (s[i] == '0') {
swap(s[i], s[lastFlipped + 1]);
lastFlipped++;
flipCount++;
}
}

return flipCount;
}

int main() {
string s = "100101000";
cout << "Binary String: " << s << endl;
return 0;
}


### Output

Binary String: 100101000


## Explanation of Test Case

Let's take a binary string "100101000" as an example. We have to remove all 0s from this string using the minimum number of non-adjacent pair flips.

• Initially, "lastFlipped" and "flipCount" are set to -1 and 0, respectively.

• We start traversing the string from left to right.

• At index 1, we encounter a '0'. We swap it with the character at index "lastFlipped + 1" (which is index 0) and increment "lastFlipped" to 0. The string becomes "010101000". The "flipCount" is incremented to 1.

• At index 4, we encounter another '0'. We swap it with the character at index "lastFlipped + 1" (which is index 1) and increment "lastFlipped" to 1. The string becomes "011010000". The "flipCount" is incremented to 2.

• At index 5, we encounter a '1'. We simply move to the next index

## Conclusion

In this article, we have discussed an algorithm to remove all 0s from a binary string using the minimum number of non-adjacent pair flips. The approach used in this algorithm is greedy, which makes it efficient and easy to implement. We have also provided a C++ code to implement the algorithm along with an example test case.

This problem can also be solved using dynamic programming, but the greedy approach provides a simpler and faster solution. The time complexity of this algorithm is O(n), where n is the length of the binary string.

In conclusion, the minimum non-adjacent pair flips algorithm is a useful tool in string manipulation and can be applied in various contexts.

Updated on: 18-May-2023

71 Views 