Saturday, 16 Oct 2021

# Minimize flips required such that string does not any pair of consecutive 0s

Minimize flips required such that string does not any pair of consecutive 0s
Given a binary string S, the task is to find the minimum count of flips required to modify a string such that it does not contain any pair of consecutive 0s.
Examples:

Input: S = “10001”Output: 1Explanation:Flipping S modifies S to “10101”.Therefore, the required output is 1.
Input: S = “100001”Output: 2Explanation:Flipping S modifies S to “110001”.Flipping S modifies S to “110101”.

Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:Iterate over the characters of the string. For every ith character, check if S[i] and S[i + 1] are equal to ‘0’ or not. If found to be true, then increment count and update S[i + 1] to ‘1’.
Finally, print the count obtained.
Below is the implementation of the above approach:

C++

#include
using namespace std;

bool cntMinOperation(string S, int N)
{

int cntOp = 0;

for (int i = 0; i < N - 1; i++) {                              if (S[i] == '0' && S[i + 1] == '0') {                             S[i + 1] = '1';                             cntOp += 1;         }     }        return cntOp; }    int main() {     string S = "10001";     int N = S.length();     cout