📜  通过删除最小元素使两个集合不相交

📅  最后修改于: 2021-10-28 01:29:26             🧑  作者: Mango

给定两组整数作为两个大小为 m 和 n 的数组。找出应该从集合中删除的最小数字的计数,以便两个集合变得不相交或不包含任何共同元素。我们可以从任何集合中删除元素。我们需要找到要删除的最小总元素。
例子 :

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

如果我们仔细观察这个问题,我们可以观察到问题简化为寻找两个集合的交集。
一个简单的解决方案是遍历第一组的每个元素,对于每个元素,检查它是否存在于第二组。如果存在,则增加要删除的元素的计数。

C++
// C++ simple program to find total elements
// to be removed to make two sets disjoint.
#include
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


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


Javascript


C++
// C++ efficient program to find total elements
// to be removed to make two sets disjoint.
#include
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  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 s = new LinkedHashSet();
        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 s = new HashSet();
        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


Javascript


输出:

2

时间复杂度: O(m * n)
辅助空间: O(1)
一个有效的解决方案是使用散列。我们创建一个空散列并将集合 1 的所有项目插入其中。现在遍历 set 2 并且对于每个元素,检查它是否存在哈希。如果存在,则增加要删除的元素的计数。

C++

// C++ efficient program to find total elements
// to be removed to make two sets disjoint.
#include
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  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 s = new LinkedHashSet();
        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

Python3

# 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 s = new HashSet();
        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

Javascript


输出:

2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程