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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right



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.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.