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++
// C++ program to find minimum elements // to be removed to make count // of 0s and 1s equal #include <bits/stdc++.h> using namespace std; // Function that returns minimum elements // need to be removed int MinRemovedElements( int a[], int b[], int n) { int no_of_ones = 0; int no_of_zeroes = 0; // Counting total number of // zeroes and ones 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++; } // Computing difference in ones and // zeroes int diff = no_of_ones - no_of_zeroes; unordered_map< int , int > mp; mp[0] = 0; int curr = 0; // Filling the difference of number // of 1's and 0's of 1st array in // unoredered_map for ( int i = 0; i < n; i++) { if (a[i] == 1) curr++; else curr--; // Condition if current difference // not found in map 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--; // Condition if current difference is // present in map then compute the // minimum one if (mp.find(diff - curr) != mp.end()) answer = min(answer, i + 1 + mp); } // Condition if the total difference // is present in 1st array if (mp.find(diff) != mp.end()) answer = min(answer, mp); return answer; } // Driver Code 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
// Java program to find minimum elements // to be removed to make count // of 0s and 1s equal import java.util.*; class GFG{ // Function that returns minimum elements // need to be removed static int MinRemovedElements( int a[], int b[], int n) { int no_of_ones = 0 ; int no_of_zeroes = 0 ; // Counting total number of // zeroes and ones 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++; } // Computing difference in ones and // zeroes int diff = no_of_ones - no_of_zeroes; HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>(); mp.put( 0 , 0 ); int curr = 0 ; // Filling the difference of number // of 1's and 0's of 1st array in // unoredered_map for ( int i = 0 ; i < n; i++) { if (a[i] == 1 ) curr++; else curr--; // Condition if current difference // not found in map 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--; // Condition if current difference is // present in map then compute the // minimum one if (mp.containsKey(diff - curr)) answer = Math.min(answer,mp.get(diff - curr) + 1 + i); } // Condition if the total difference // is present in 1st array if (mp.containsKey(diff)) answer = Math.min(answer, mp.get(diff)); return answer; } // Driver Code 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" ); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to find minimum # elements to be removed to make # count of 0s and 1s equal # Function that returns minimum # elements need to be removed def MinRemovedElements(a, b, n): no_of_ones = 0 ; no_of_zeroes = 0 ; # Counting total number of # zeroes and ones 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 ; # Computing difference in ones and # zeroes diff1 = no_of_ones - no_of_zeroes; mp = {}; mp[ 0 ] = 0 ; curr = 0 ; # Filling the difference of number # of 1's and 0's of 1st array in # unoredered_map for i in range (n): if (a[i] = = 1 ): curr + = 1 ; else : curr - = 1 ; # Condition if current difference # not found in map 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 ; # Condition if current difference is # present in map then compute the # minimum one if (diff1 - curr) in mp : answer = min (answer, i + 1 + mp[diff1 - curr]); # Condition if the total difference # is present in 1st array if diff1 in mp: answer = min (answer, mp[diff1]); return answer; # Driver Code 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)); # This code is contributed by Yash_R |
C#
// C# program to find minimum elements // to be removed to make count // of 0s and 1s equal using System; using System.Collections.Generic; class GFG{ // Function that returns minimum elements // need to be removed static int MinRemovedElements( int [] a, int [] b, int n) { int no_of_ones = 0; int no_of_zeroes = 0; // Counting total number of // zeroes and ones 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++; } // Computing difference in ones and // zeroes int diff = no_of_ones - no_of_zeroes; Dictionary< int , int > mp = new Dictionary< int , int >(); mp.Add(0, 0); int curr = 0; // Filling the difference of number // of 1's and 0's of 1st array in // unoredered_map for ( int i = 0; i < n; i++) { if (a[i] == 1) curr++; else curr--; // Condition if current difference // not found in map 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--; // Condition if current difference is // present in map then compute the // minimum one if (mp.ContainsKey(diff - curr)) answer = Math.Min(answer, i + 1 + mp); } // Condition if the total difference // is present in 1st array if (mp.ContainsKey(diff)) answer = Math.Min(answer, mp); return answer; } // Driver code 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)); } } // This code is contributed by divyeshrabadiya07 |
Javascript
<script> // Javascript program to find minimum elements // to be removed to make count // of 0s and 1s equal // Function that returns minimum elements // need to be removed function MinRemovedElements(a, b, n) { let no_of_ones = 0; let no_of_zeroes = 0; // Counting total number of // zeroes and ones 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++; } // Computing difference in ones and // zeroes let diff = no_of_ones - no_of_zeroes; let mp = new Map(); mp.set(0 , 0); let curr = 0; // Filling the difference of number // of 1's and 0's of 1st array in // unoredered_map for (let i = 0; i < n; i++) { if (a[i] == 1) curr++; else curr--; // Condition if current difference // not found in map 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--; // Condition if current difference is // present in map then compute the // minimum one if (mp.has(diff - curr)) answer = Math.min(answer, mp.get(diff - curr) + i + 1); } // Condition if the total difference // is present in 1st array if (mp.has(diff)) answer = Math.min(answer, mp.get(diff)); return answer; } // Driver Code 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>" ); // This code is contributed by _saurabh_jaiswal </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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!