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


Here, we require to manipulate the binary string so that it doesn’t contain any consecutive zeros. If we find consecutive zeros, we need to change any zero to 1. So, we require to count the total number of 0 to 1 conversion we should make to remove all consecutive zeros from the string.

Problem statement − We have given a binary string ‘str’ containing only 0 and 1. We require to find the minimum number of flips required so that the resultant string doesn’t contain any consecutive zeros.

Sample Examples

Input –  0101001
Output – 1

Explanation

We require to flip only the last second zero.

Input –  str = 00100000100
Output – 4

Explanation

We require to flip the 2nd, 5th, 7th, and 11th zero to remove all pairs of consecutive zeros.

Input –  0101010101
Output – 0

Explanation

We need to perform zero flips as the string contains no consecutive zeros.

Approach 1

In this approach, we will iterate through the string from start to end and flip the characters if it contains any consecutive zeros. In the end, we can get the total number of minimum flips require to remove all consecutive zeros from the given binary string.

Algorithm

  • Step 1 − Initialize the ‘cnt’ variable with zero, storing a total number of flips.

  • Step 2 − Iterate through the binary string using the loop.

  • Step 3 − If the character at index ‘I’ and index ‘I +1’ is 0, flip the next character, and increase the value of the ‘cnt’ variable by 1.

  • Step 4 − When iteration of for loop completes, return the value of ‘cnt’.

Example

#include <bits/stdc++.h>
using namespace std;
// Function to get the total number of minimum flips required so the string does not contain any consecutive 0’s
int findCount(string str, int len){

   // initialize the count
   int cnt = 0;
   
   // iterate over the string
   for (int i = 0; i < len - 1; i++){
   
      // If two consecutive characters are the same
      if (str[i] == '0' && str[i + 1] == '0'){
      
         // Flip the next character
         str[i + 1] = '1';
         
         // increment count
         cnt++;           
      }
   }
   return cnt;
}
int main(){
   string str = "00100000100";
   int len = str.length();
   cout << "The minimum numbers of flips required so that string doesn't contain any pair of consecutive zeros - " << findCount(str, len);
   return 0;
}

Output

The minimum numbers of flips required so that string doesn't contain any pair of consecutive zeros - 4

Time complexity − O(N), as we iterate through the binary string.

Space complexity − O(1), as we use constant space to store total counts.

Conclusion

We learned to count minimum flips to remove the consecutive pairs of zeros from the given binary string. Users can try to count minimum flips to remove the consecutive pairs of ones from the given binary string.

Updated on: 28-Jul-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements