Minimum Removals Required To Place All 0s Before 1s In a Binary String


Problem Statement

We have given binary string str, and we require to remove minimum characters from the string such that we can place all zeros before 1.

Sample Examples

Input

str = ‘00110100111’

Output

3

Explanation

Here, we can achieve output 3 in two ways.

We can either remove arr[2], arr[3], and arr[5] or arr[4], arr[6], and arr[7] from the string.

Input

str = ‘001101011’

Output

2

Explanation

We can remove arr[4] and arr[6] to place all zeros before 1.

Input

str = ‘000111’

Output

0

Explanation

In the given str, all zeros are already placed before the 1s, so we don’t require removing any characters from the given string.

Approach 1

In the first approach, we will use two arrays. The first array stores all ones on the left, and another array stores all zeros on the right. After that, we can add elements at the ith index in both arrays and find the minimum sum.

Algorithm

  • Step 1 − Define ‘zeros’ and ‘ones’ named list of length n, where n is the length of the string which we can get using the size() method.

  • Step 2 − If the first character in the given string is ‘1’, store 1 at the 0th index of the ‘ones’ array; Otherwise, store 0.

  • Step 3 − Use the for loop to traverse through from the second element of the string. In the for loop, initialize the ones[i] with a resultant value which we get by adding ones[i-1] and 1 or 0 according to the character of the string at ith index.

  • Step 4 − Store 1 or 0 at zeros[n-1] according to the last character in the given string.

  • Step 5 − Use the for loop to traverse the string from the last second character and update the value of the list of the zeros according to the string character.

  • Step 6 − Define the ‘min_zeros_to_remove’ variable and initialize it with the maximum integer value.

  • Step 7 − Now, traverse through the ‘zeros’ and ‘ones’ lists. Access the value from the ‘i+1’ index from the zeros list and the ‘I’ index from the ‘ones’ list. After that, add both elements.

  • Step 8 − Change the value of ‘min_zeros_to_remove’ with the sum of both array elements if it is lesser than the current value of the ‘min_zeros_to_remove’ variable.

  • Step 9 − Return the resultant value.

Example

#include <bits/stdc++.h>
using namespace std;

int minimumZeroRemoval(string str){
   int n = str.size();
   // arrays to store the prefix sums of zeros and ones
   vector<int> zeros(n);
   vector<int> ones(n);
   // compute total number of 1s at the left of each index
   ones[0] = (str[0] == '1') ? 1 : 0;
   for (int i = 1; i < n; i++) {
      ones[i] = ones[i - 1] + ((str[i] == '1') ? 1 : 0);
   }
   // compute total number of 0s at the right of each index
   zeros[n - 1] = (str[n - 1] == '0') ? 1 : 0;
   for (int i = n - 2; i >= 0; i--){
      zeros[i] = zeros[i + 1] + ((str[i] == '0') ? 1 : 0);
   }
   // do the sum of zeros and ones for each index and return the minimum value
   int min_zeros_to_remove = INT_MAX;
   for (int i = 0; i < n - 1; i++){
      min_zeros_to_remove = min(min_zeros_to_remove, zeros[i + 1] + ones[i]);
   }
   return min_zeros_to_remove;
}
int main() {
   string str = "00110100111";
   int count = minimumZeroRemoval(str);
   cout << "The minimum number of zeros required to remove from the given string is - " << count;
   return 0;
}

Output

The minimum number of zeros required to remove from the given string is - 3
  • Time complexity − O(N), as we need for loop to traverse through strings and lists of size N.

  • Space complexity − O(N), as we use two lists to store the count of ones and zeros.

Approach 2

This approach is the optimized version of the first approach. Here, we use two variables to store the count of ones and zeros rather than the lists.

Algorithm

  • Step 1 − Define the ‘zeros_right’ variable and initialize with 0.

  • Step 2 − Traverse through the string, count the total numbers of ‘0’ characters in the given string, and update the value of the ‘zero_right’ variable according to that.

  • Step 3 − Define the ‘ones_left’ variable and initialize with 0.

  • Step 4 − Traverse through the string using the for loop. If the character at the ith index in the string is ‘1’, increment the value of the ‘ones_left’ variable by 1. Otherwise, decrease the value of the ‘zeros_right’ by 1.

  • Step 5 − Update the value of the ‘res’ variable if the ‘zeros_right + ones_left’ is less than the ‘res’ variable’s current value.

Example

#include <bits/stdc++.h>
using namespace std;

// function to remove zeros from the string to place all zeros before 1.
int minimumZeroRemoval(string str){
   // counting the total number of zeros in the given string
   int zeros_right = 0;
   for (int i = 0; i < str.size(); i++) {
      if (str[i] == '0')
      zeros_right += 1;
   }
   // variable to store the number of ones from left
   int ones_left = 0;
   // Size of the string
   int len = str.size();
   // variable to store the final result
   int result = INT_MAX;
   // Traverse the string from left to right
   for (int i = 0; i < len; i++){
      // If the current character is '1', then increment ones_left by 1 else, decrement zeros_right by 1
      if (str[i] == '1') {
         ones_left += 1;
      } else {
         zeros_right -= 1;
      }
      // Store the minimum result and zeros_right + ones_left in result
      result = min(result, zeros_right + ones_left);
   }
   // Return the final result
   return result;
}
int main() {
   string str = "001101011";
   int count = minimumZeroRemoval(str);
   cout << "The minimum number of zeros required to remove from the given string is - " << count;
   return 0;
}

Output

The minimum number of zeros required to remove from the given string is - 2
  • Time complexity − O(N), as we iterate through the string.

  • Space complexity − O(1), as we use only constant space.

Conclusion

Users learned two approaches to removing minimum characters from the given binary string. The code of the second approach is more readable as we use variables to store the count of zeros and ones instead of using the list.

Updated on: 18-Jul-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements