GFG App
Open App
Browser
Continue

# Minimum removals to segregate all 0s and 1s such that count of 0s and 1s are equal

Given a binary string S of length N, the task is to find the minimum number of removals to segregate all the 0s and all the 1s such that all the 0s are before all the 1s and their count are also same.

Examples:

Input: S = 01001101
Output: 2
Explanation: If you remove the 1 at index 1 and
0 at index 6, then the string becomes 000111.

Input: S = “01”
Output: 0

Approach: The problem can be solved based on the concept of prefix sum:

Instead of trying to find the minimum number of removals find the maximum length subsequence which satisfies the condition.
To do this,

• Firstly calculate prefix sum for 0s and suffix sum for 1s and
• For any index i find the maximum length of the string that can be formed using 0s from the left of i and 1s from the right of i when their count are equal.
• The maximum among these lengths, is the subsequence of maximum size satisfying the criteria.
• The remaining elements must be removed.

Follow the below steps to solve the problem:

• Store prefix count of ‘0‘ in a vector left.
• Store Suffix count of ‘1‘ in a vector right.
• Initialize a variable Max = 0.
• Run a loop from index 1 to N-1.
• Check if left[i-1] = right[i], then update, Max = max(Max, 2*right[i]).
• Return N-Max.

Below is the implementation of the above approach:

## C++

 // C++ code to implement the above approach   #include using namespace std;   // Function to remove minimum number // of brackets to make the string beautiful int findMinRemoval(string s) {     int n = s.size();       // Stores count of 0 from left to right     vector left(n);       // Stores count of 1 from right to left     vector right(n);       if (s[0] == '0')         left[0] = 1;     else         left[0] = 0;       // Calculate the prefix count of '0'     // at every index     for (int i = 1; i < n; i++) {         if (s[i] == '0')             left[i] = left[i - 1] + 1;         else             left[i] = left[i - 1];     }       if (s[n - 1] == '1')         right[n - 1] = 1;     else         right[n - 1] = 0;       // Calculate the suffix count of '1'     // at every index     for (int i = n - 2; i >= 0; i--) {         if (s[i] == '1')             right[i] = right[i + 1] + 1;         else             right[i] = right[i + 1];     }       int Max = 0;       // Check for max length beautiful string     for (int i = 1; i < n; i++) {         if (left[i - 1] == right[i])             Max = max(Max, 2 * right[i]);     }     return n - Max; }   // Driver Code int main() {     string S = "01001101";       // Function call     cout << findMinRemoval(S);     return 0; }

## Java

 // Java code to implement the above approach import java.io.*;   class GFG {     // Function to remove minimum number     // of brackets to make the string beautiful     public static int findMinRemoval(String s)     {         int n = s.length();           // Stores count of 0 from left to right         int left[] = new int[n];           // Stores count of 1 from right to left         int right[] = new int[n];           if (s.charAt(0) == '0')             left[0] = 1;         else             left[0] = 0;           // Calculate the prefix count of '0'         // at every index         for (int i = 1; i < n; i++) {             if (s.charAt(i) == '0')                 left[i] = left[i - 1] + 1;             else                 left[i] = left[i - 1];         }           if (s.charAt(n - 1) == '1')             right[n - 1] = 1;         else             right[n - 1] = 0;           // Calculate the suffix count of '1'         // at every index         for (int i = n - 2; i >= 0; i--) {             if (s.charAt(i) == '1')                 right[i] = right[i + 1] + 1;             else                 right[i] = right[i + 1];         }           int Max = 0;           // Check for max length beautiful string         for (int i = 1; i < n; i++) {             if (left[i - 1] == right[i])                 Max = Math.max(Max, 2 * right[i]);         }         return n - Max;     }       // Driver Code     public static void main(String[] args)     {         String S = "01001101";           // Function call         System.out.print(findMinRemoval(S));     } }   // This code is contributed by Rohit Pradhan

## Python3

 # Python3 code to implement the above approach   # Function to remove minimum number # of brackets to make the string beautiful def findMinRemoval(s):       n = len(s)       # Stores count of 0 from left to right     left = [0] * n       # Stores count of 1 from right to left     right = [0] * n       if (s[0] == '0'):         left[0] = 1     else:         left[0] = 0       # Calculate the prefix count of '0'     # at every index     for i in range(1, n):         if (s[i] == '0'):             left[i] = left[i - 1] + 1         else:             left[i] = left[i - 1];       if (s[n - 1] == '1'):         right[n - 1] = 1     else:         right[n - 1] = 0       # Calculate the suffix count of '1'     # at every index     for i in range(n-2, -1, -1):         if (s[i] == '1'):             right[i] = right[i + 1] + 1         else:             right[i] = right[i + 1]       Max = 0       # Check for max length beautiful string     for i in range(1,n):         if (left[i - 1] == right[i]):             Max = max(Max, 2 * right[i])     return n - Max;   # Driver Code if __name__ == "__main__":       S = "01001101"       # Function call     print(findMinRemoval(S))       # This code is contributed by hrithikgarg03188.

## C#

 // C# program for above approach: using System; class GFG {     // Function to remove minimum number   // of brackets to make the string beautiful   static int findMinRemoval(string s)   {     int n = s.Length;       // Stores count of 0 from left to right     int[] left = new int[n];       // Stores count of 1 from right to left     int[] right = new int[n];       if (s[0] == '0')       left[0] = 1;     else       left[0] = 0;       // Calculate the prefix count of '0'     // at every index     for (int i = 1; i < n; i++) {       if (s[i] == '0')         left[i] = left[i - 1] + 1;       else         left[i] = left[i - 1];     }       if (s[n - 1] == '1')       right[n - 1] = 1;     else       right[n - 1] = 0;       // Calculate the suffix count of '1'     // at every index     for (int i = n - 2; i >= 0; i--) {       if (s[i] == '1')         right[i] = right[i + 1] + 1;       else         right[i] = right[i + 1];     }       int Maxx = 0;       // Check for max length beautiful string     for (int i = 1; i < n; i++) {       if (left[i - 1] == right[i])         Maxx = Math.Max(Maxx, 2 * right[i]);     }     return n - Maxx;   }     // Driver Code   public static void Main()   {     String S = "01001101";       // Function call     Console.Write(findMinRemoval(S));   } }   // This code is contributed by code_hunt.

## Javascript



Output

2

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up