Minimum removals to segregate all 0s and 1s such that count of 0s and 1s are equal
Last Updated :
04 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int findMinRemoval(string s)
{
int n = s.size();
vector< int > left(n);
vector< int > right(n);
if (s[0] == '0' )
left[0] = 1;
else
left[0] = 0;
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;
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;
for ( int i = 1; i < n; i++) {
if (left[i - 1] == right[i])
Max = max(Max, 2 * right[i]);
}
return n - Max;
}
int main()
{
string S = "01001101" ;
cout << findMinRemoval(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findMinRemoval(String s)
{
int n = s.length();
int left[] = new int [n];
int right[] = new int [n];
if (s.charAt( 0 ) == '0' )
left[ 0 ] = 1 ;
else
left[ 0 ] = 0 ;
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 ;
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 ;
for ( int i = 1 ; i < n; i++) {
if (left[i - 1 ] == right[i])
Max = Math.max(Max, 2 * right[i]);
}
return n - Max;
}
public static void main(String[] args)
{
String S = "01001101" ;
System.out.print(findMinRemoval(S));
}
}
|
Python3
def findMinRemoval(s):
n = len (s)
left = [ 0 ] * n
right = [ 0 ] * n
if (s[ 0 ] = = '0' ):
left[ 0 ] = 1
else :
left[ 0 ] = 0
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
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
for i in range ( 1 ,n):
if (left[i - 1 ] = = right[i]):
Max = max ( Max , 2 * right[i])
return n - Max ;
if __name__ = = "__main__" :
S = "01001101"
print (findMinRemoval(S))
|
C#
using System;
class GFG {
static int findMinRemoval( string s)
{
int n = s.Length;
int [] left = new int [n];
int [] right = new int [n];
if (s[0] == '0' )
left[0] = 1;
else
left[0] = 0;
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;
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;
for ( int i = 1; i < n; i++) {
if (left[i - 1] == right[i])
Maxx = Math.Max(Maxx, 2 * right[i]);
}
return n - Maxx;
}
public static void Main()
{
String S = "01001101" ;
Console.Write(findMinRemoval(S));
}
}
|
Javascript
<script>
const findMinRemoval = (s) => {
let n = s.length;
let left = new Array(n).fill(0);
let right = new Array(n).fill(0);
if (s[0] == '0' )
left[0] = 1;
else
left[0] = 0;
for (let 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;
for (let i = n - 2; i >= 0; i--) {
if (s[i] == '1' )
right[i] = right[i + 1] + 1;
else
right[i] = right[i + 1];
}
let Max = 0;
for (let i = 1; i < n; i++) {
if (left[i - 1] == right[i])
Max = Math.max(Max, 2 * right[i]);
}
return n - Max;
}
let S = "01001101" ;
document.write(findMinRemoval(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...