Make two sets disjoint by removing minimum elements
Given two sets of integers as two arrays of size m and n. Find count of minimum numbers that should be removed from the sets so that both set become disjoint or don’t contains any elements in common. We can remove elements from any set. We need to find minimum total elements to be removed.
Examples :
Input : set1[] = {20, 21, 22}
set2[] = {22, 23, 24, 25}
Output : 1
We need to remove at least 1 element
which is 22 from set1 to make two
sets disjoint.
Input : set1[] = {20, 21, 22}
set2[] = {22, 23, 24, 25, 20}
Output : 2
Input : set1[] = {6, 7}
set2[] = {12, 13, 14, 15}
Output : 0
If we take a closer look at this problem, we can observe that the problem reduces to finding intersection of two sets.
A simple solution is iterate through every element of first set and for every element, check if it is present in second set. If present, increment count of elements to be removed.
C++
// C++ simple program to find total elements // to be removed to make two sets disjoint. #include<bits/stdc++.h> using namespace std; // Function for find minimum elements to be // removed from both sets so that they become // disjoint int makeDisjoint(int set1[], int set2[], int n, int m) { int result = 0; for (int i=0; i<n; i++) { int j; for (j=0; j<m; j++) if (set1[i] == set2[j]) break; if (j != m) result++; } return result; } // Driven code int main() { int set1[] = {20, 21, 22}; int set2[] = {22, 23, 24, 25, 20}; int n = sizeof(set1)/sizeof(set1[0]); int m = sizeof(set2)/sizeof(set2[1]); cout << makeDisjoint(set1, set2, n, m); return 0; } |
Java
// Java simple program to find // total elements to be removed // to make two sets disjoint. import java.io.*; public class GFG{ // Function for find minimum elements // to be removed from both sets // so that they become disjoint static int makeDisjoint(int []set1, int []set2, int n, int m) { int result = 0; for (int i = 0; i < n; i++) { int j; for (j = 0; j < m; j++) if (set1[i] == set2[j]) break; if (j != m) result++; } return result; } // Driver code static public void main (String[] args) { int []set1 = {20, 21, 22}; int []set2 = {22, 23, 24, 25, 20}; int n = set1.length; int m = set2.length; System.out.println(makeDisjoint(set1, set2, n, m)); } } // This code is contributed by vt_m. |
Python 3
# Python 3 simple program to find # total elements to be removed to # make two sets disjoint. # Function for find minimum elements # to be removed from both sets so that # they become disjoint def makeDisjoint(set1, set2, n, m): result = 0; for i in range (0, n - 1): for j in range(0, m - 1): if (set1[i] == set2[j]): break if (j != m): result += 1 return result # Driver Code set1 = [20, 21, 22] set2 = [22, 23, 24, 25, 20] n = len(set1) m = len(set2) print(makeDisjoint(set1, set2, n, m)) # This code is contributed # by Akanksha Rai |
C#
// C# simple program to find // total elements to be removed // to make two sets disjoint. using System; public class GFG{ // Function for find minimum elements // to be removed from both sets // so that they become disjoint static int makeDisjoint(int []set1, int []set2, int n, int m) { int result = 0; for (int i = 0; i < n; i++) { int j; for (j = 0; j < m; j++) if (set1[i] == set2[j]) break; if (j != m) result++; } return result; } // Driver code static public void Main () { int []set1 = {20, 21, 22}; int []set2 = {22, 23, 24, 25, 20}; int n = set1.Length; int m = set2.Length; Console.WriteLine(makeDisjoint(set1, set2, n, m)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP simple program to find // total elements to be removed // to make two sets disjoint. // Function for find minimum // elements to be removed from // both sets so that they become disjoint function makeDisjoint( $set1, $set2, $n, $m) { $result = 0; for ( $i = 0; $i < $n; $i++) { $j; for ($j = 0; $j < $m; $j++) if ($set1[$i] == $set2[$j]) break; if ($j != $m) $result++; } return $result; } // Driven code $set1 = array(20, 21, 22); $set2 = array(22, 23, 24, 25, 20); $n = count($set1); $m = count($set2); echo makeDisjoint($set1, $set2, $n, $m); // This code is contributed by anuj_67. ?> |
Output :
2
Time complexity : O(m * n)
Auxiliary Space : O(1)
An efficient solution is to use hashing. We create an empty hash and insert all items of set 1 in it. Now iterate through set 2 and for every element, check if it is present is hash. If present, increment count of elements to be removed.
C++
// C++ efficient program to find total elements // to be removed to make two sets disjoint. #include<bits/stdc++.h> using namespace std; // Function for find minimum elements to be // removed from both sets so that they become // disjoint int makeDisjoint(int set1[], int set2[], int n, int m) { // Store all elements of first array in a hash // table unordered_set <int> s; for (int i = 0; i < n; i++) s.insert(set1[i]); // We need to remove all those elements from // set2 which are in set1. int result = 0; for (int i = 0; i < m; i++) if (s.find(set2[i]) != s.end()) result++; return result; } // Driver code int main() { int set1[] = {20, 21, 22}; int set2[] = {22, 23, 24, 25, 20}; int n = sizeof(set1)/sizeof(set1[0]); int m = sizeof(set2)/sizeof(set2[1]); cout << makeDisjoint(set1, set2, n, m); return 0; } |
Java
// Java efficient program to find total elements // to be removed to make two sets disjoint. import java.util.*; class GFG { // Function for find minimum elements to be // removed from both sets so that they become // disjoint static int makeDisjoint(int set1[], int set2[], int n, int m) { // Store all elements of first array in a hash // table LinkedHashSet<Integer> s = new LinkedHashSet<Integer>(); for (int i = 0; i < n; i++) { s.add(set1[i]); } // We need to remove all those elements from // set2 which are in set1. int result = 0; for (int i = 0; i < m; i++) { if (s.contains(set2[i])) { result++; } } return result; } // Driver code public static void main(String[] args) { int set1[] = {20, 21, 22}; int set2[] = {22, 23, 24, 25, 20}; int n = set1.length; int m = set2.length; System.out.println(makeDisjoint(set1, set2, n, m)); } } // This code is contributed by PrinciRaj1992 |
Python 3
# Python 3 efficient program to find # total elements to be removed to # make two sets disjoint. # Function for find minimum elements # to be removed from both sets so # that they become disjoint def makeDisjoint(set1, set2, n, m): # Store all elements of first array # in a hash table s = [] for i in range(n): s.append(set1[i]) # We need to remove all those elements # from set2 which are in set1. result = 0 for i in range(m): if set2[i] in s: result += 1 return result # Driver code if __name__ =="__main__": set1 = [20, 21, 22] set2 = [22, 23, 24, 25, 20] n = len(set1) m = len(set2) print(makeDisjoint(set1, set2, n, m)) # This code is contributed # by ChitraNayal |
C#
// C# efficient program to find total elements // to be removed to make two sets disjoint. using System; using System.Collections.Generic; class GFG { // Function for find minimum elements to be // removed from both sets so that they become // disjoint static int makeDisjoint(int []set1, int []set2, int n, int m) { // Store all elements of first array in a hash // table HashSet<int> s = new HashSet<int>(); for (int i = 0; i < n; i++) { s.Add(set1[i]); } // We need to remove all those elements from // set2 which are in set1. int result = 0; for (int i = 0; i < m; i++) { if (s.Contains(set2[i])) { result++; } } return result; } // Driver code public static void Main(String[] args) { int []set1 = {20, 21, 22}; int []set2 = {22, 23, 24, 25, 20}; int n = set1.Length; int m = set2.Length; Console.WriteLine(makeDisjoint(set1, set2, n, m)); } } // This code is contributed by Rajput-Ji |
Output :
2
Time complexity : O(m + n)
Auxiliary Space : O(m)
This article is contributed by smarakchopdar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Minimum cost to make array size 1 by removing larger of pairs
- How to check if two given sets are disjoint?
- Minimum number of distinct elements after removing m items
- Minimum array element changes to make its elements 1 to N
- Minimum gcd operations to make all array elements one
- Minimum replacements to make elements of a ternary array same
- Minimum delete operations to make all elements of array same
- Minimum steps to make all the elements of the array divisible by 4
- Minimum number of moves to make all elements equal
- Minimum cost to make all array elements equal
- Minimum number of operations on an array to make all elements 0
- Minimum increment by k operations to make all elements equal
- Make all array elements equal with minimum cost
- Minimum operation to make all elements equal in array
- Minimum no. of operations required to make all Array Elements Zero


