Minimum elements to be removed to make count of 0s and 1s equal
Last Updated :
09 Sep, 2022
Given two binary arrays A and B of equal length, the task is to find the minimum number of elements to be removed, from the front, to make the count of 0s and 1s equal in the given two Binary Arrays.
Examples:
Input: A = {0, 1, 0, 1, 1, 0}, B = {1, 0, 0, 1, 1, 1}
Output: 6
Explanation:
-> Remove first 5 elements from array A
-> Remove the first element from B.
-> Therefore at least 6 elements are needed to be removed in order to make the total number of 1’s and 0’s equal.
Input: a = {1, 0}, b = {0, 1}
Output: 0
Explanation:
The count of 1s and 0s are already equal. Therefore no element needs to be removed.
Approach: The idea is to find the difference between the total number of 1’s and the total number of 0’s separately and minimising this difference. Removing 1 will decrease the difference by 1 and removing 0 will increase diff by 1.
Therefore, the steps needed to make the difference equal to 0 are:
- Count initial difference in total number of 1’s and 0’s. Let’s call it initial_diff.
- Remove first l elements from the 1st array and store the difference in a variable left_diff.
- Remove first r elements from the 2nd array and store the difference in a variable right_diff.
- Now find such values of l and r such that:
initial_diff – left_diff – right_diff = 0.
- In order to find l and r optimally, for each unique value of right_diff, save the smallest r to reach this value in an unordered_map. Then finally, iterate over l to find the minimum answer.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int MinRemovedElements( int a[], int b[],
int n)
{
int no_of_ones = 0;
int no_of_zeroes = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
if (b[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
}
int diff = no_of_ones - no_of_zeroes;
unordered_map< int , int > mp;
mp[0] = 0;
int curr = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 1)
curr++;
else
curr--;
if (mp.find(curr) == mp.end())
mp[curr] = i + 1;
}
curr = 0;
int answer = 2 * n;
for ( int i = 0; i < n; i++) {
if (b[i] == 1)
curr++;
else
curr--;
if (mp.find(diff - curr) != mp.end())
answer
= min(answer,
i + 1 + mp);
}
if (mp.find(diff) != mp.end())
answer = min(answer, mp);
return answer;
}
int main()
{
int a[] = { 0, 1, 0, 1, 1, 0 };
int b[] = { 1, 0, 0, 1, 1, 1 };
int size = sizeof (a) / sizeof (a[0]);
cout << MinRemovedElements(a, b, size)
<< "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MinRemovedElements( int a[], int b[],
int n)
{
int no_of_ones = 0 ;
int no_of_zeroes = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 1 )
no_of_ones++;
else
no_of_zeroes++;
if (b[i] == 1 )
no_of_ones++;
else
no_of_zeroes++;
}
int diff = no_of_ones - no_of_zeroes;
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
mp.put( 0 , 0 );
int curr = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 1 )
curr++;
else
curr--;
if (!mp.containsKey(curr))
mp.put(curr, i + 1 );
}
curr = 0 ;
int answer = 2 * n;
for ( int i = 0 ; i < n; i++) {
if (b[i] == 1 )
curr++;
else
curr--;
if (mp.containsKey(diff - curr))
answer
= Math.min(answer,mp.get(diff - curr) + 1 + i);
}
if (mp.containsKey(diff))
answer = Math.min(answer, mp.get(diff));
return answer;
}
public static void main(String[] args)
{
int a[] = { 0 , 1 , 0 , 1 , 1 , 0 };
int b[] = { 1 , 0 , 0 , 1 , 1 , 1 };
int size = a.length;
System.out.print(MinRemovedElements(a, b, size)
+ "\n" );
}
}
|
Python3
def MinRemovedElements(a, b, n):
no_of_ones = 0 ;
no_of_zeroes = 0 ;
for i in range (n):
if (a[i] = = 1 ):
no_of_ones + = 1 ;
else :
no_of_zeroes + = 1 ;
if (b[i] = = 1 ):
no_of_ones + = 1 ;
else :
no_of_zeroes + = 1 ;
diff1 = no_of_ones - no_of_zeroes;
mp = {};
mp[ 0 ] = 0 ;
curr = 0 ;
for i in range (n):
if (a[i] = = 1 ):
curr + = 1 ;
else :
curr - = 1 ;
if curr not in mp:
mp[curr] = i + 1 ;
curr = 0 ;
answer = 2 * n;
for i in range (n):
if (b[i] = = 1 ):
curr + = 1 ;
else :
curr - = 1 ;
if (diff1 - curr) in mp :
answer = min (answer, i + 1 + mp[diff1 -
curr]);
if diff1 in mp:
answer = min (answer, mp[diff1]);
return answer;
if __name__ = = "__main__" :
a = [ 0 , 1 , 0 , 1 , 1 , 0 ];
b = [ 1 , 0 , 0 , 1 , 1 , 1 ];
size = len (a);
print (MinRemovedElements(a, b, size));
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MinRemovedElements( int [] a, int [] b,
int n)
{
int no_of_ones = 0;
int no_of_zeroes = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
if (b[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
}
int diff = no_of_ones - no_of_zeroes;
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
mp.Add(0, 0);
int curr = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 1)
curr++;
else
curr--;
if (!mp.ContainsKey(curr))
mp.Add(curr, i + 1);
}
curr = 0;
int answer = 2 * n;
for ( int i = 0; i < n; i++)
{
if (b[i] == 1)
curr++;
else
curr--;
if (mp.ContainsKey(diff - curr))
answer = Math.Min(answer,
i + 1 + mp);
}
if (mp.ContainsKey(diff))
answer = Math.Min(answer, mp);
return answer;
}
static void Main()
{
int [] a = { 0, 1, 0, 1, 1, 0 };
int [] b = { 1, 0, 0, 1, 1, 1 };
int size = a.Length;
Console.WriteLine(MinRemovedElements(
a, b, size));
}
}
|
Javascript
<script>
function MinRemovedElements(a, b, n)
{
let no_of_ones = 0;
let no_of_zeroes = 0;
for (let i = 0; i < n; i++) {
if (a[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
if (b[i] == 1)
no_of_ones++;
else
no_of_zeroes++;
}
let diff = no_of_ones - no_of_zeroes;
let mp = new Map();
mp.set(0 , 0);
let curr = 0;
for (let i = 0; i < n; i++) {
if (a[i] == 1)
curr++;
else
curr--;
if (!mp.has(curr))
mp.set(curr, i + 1);
}
curr = 0;
let answer = 2 * n;
for (let i = 0; i < n; i++) {
if (b[i] == 1)
curr++;
else
curr--;
if (mp.has(diff - curr))
answer = Math.min(answer, mp.get(diff - curr) + i + 1);
}
if (mp.has(diff))
answer = Math.min(answer, mp.get(diff));
return answer;
}
let a = [ 0, 1, 0, 1, 1, 0 ];
let b = [ 1, 0, 0, 1, 1, 1 ];
let size = a.length;
document.write(MinRemovedElements(a, b, size) + "<br>" );
</script>
|
Output:
6
Time complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for storing the elements in the HashMap.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...