📜  以 M 为最小缺失数的最大子集

📅  最后修改于: 2021-10-26 06:44:44             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[]和一个正整数M ,任务是找到最小缺失整数为M的最长子集的长度。如果不存在这样的子集,则打印“-1”

例子:

朴素的方法:最简单的方法是生成给定数组的所有可能子集,并存储那些最小缺失整数为 M 的子集中的最大长度。最后,打印最大长度作为答案。

时间复杂度: O(N*2 N )
辅助空间: O(N)

有效的方法:
要优化上述方法,请考虑以下观察结果。如果数组中没有小于M 的元素丢失,则最大子集的大小将为 1,由除M之外的所有数组元素组成。否则,不可能有最小缺失数M 的子集。

请按照以下步骤操作:

  1. 将数组中除M之外的所有元素插入到集合中。
  2. 查找数组arr[]中不等于M的元素(例如cnt )的频率。
  3. 找到集合中的最小缺失数,如果它等于M ,则将cnt的值打印为子集的最大大小。
  4. 否则,打印“-1” ,因为不可能有最小缺失数为M 的子集。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
#define ll long long int
using namespace std;
 
// Function to find and return the
// length of the longest subset
// whose smallest missing value is M
ll findLengthOfMaxSubset(int arr[],
                        int n, int m)
{
    // Initialize a set
    set s;
 
    int answer = 0;
 
    for (int i = 0; i < n; i++) {
 
        int tmp = arr[i];
 
        // If array element is not
        // equal to M
        if (tmp != m) {
 
            // Insert into set
            s.insert(tmp);
 
            // Increment frequency
            answer++;
        }
    }
 
    // Stores minimum missing
    // number
    int min = 1;
 
    // Iterate to find the
    // minimum missing
    // integer
    while (s.count(min)) {
        min++;
    }
 
    // If minimum obtained
    // is less than M
    if (min != m) {
 
        // Update answer
        answer = -1;
    }
 
    // Return answer
    return answer;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int M = 3;
 
    cout << findLengthOfMaxSubset(
        arr, N, M);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Function to find and return the
// length of the longest subset
// whose smallest missing value is M
static int findLengthOfMaxSubset(int arr[],
                                int n, int m)
{
     
    // Initialize a set
    Set s = new HashSet<>();
 
    int answer = 0;
 
    for(int i = 0; i < n; i++)
    {
        int tmp = arr[i];
 
        // If array element is not
        // equal to M
        if (tmp != m)
        {
             
            // Insert into set
            s.add(tmp);
 
            // Increment frequency
            answer++;
        }
    }
     
    // Stores minimum missing
    // number
    int min = 1;
 
    // Iterate to find the
    // minimum missing
    // integer
    while (s.contains(min))
    {
        min++;
    }
 
    // If minimum obtained
    // is less than M
    if (min != m)
    {
         
        // Update answer
        answer = -1;
    }
 
    // Return answer
    return answer;
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 4 };
    int N = arr.length;
    int M = 3;
     
    System.out.print(findLengthOfMaxSubset(
        arr, N, M));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to find and return the
# length of the longest subset
# whose smallest missing value is M
def findLengthOfMaxSubset(arr, n, m):
     
    # Initialize a set
    s = [];
 
    answer = 0;
 
    for i in range(n):
        tmp = arr[i];
 
        # If array element is not
        # equal to M
        if (tmp != m):
 
            # Insert into set
            s.append(tmp);
 
            # Increment frequency
            answer += 1;
 
    # Stores minimum missing
    # number
    min = 1;
 
    # Iterate to find the
    # minimum missing
    # integer
    while (s.count(min)):
        min += 1;
 
    # If minimum obtained
    # is less than M
    if (min != m):
 
        # Update answer
        answer = -1;
 
    # Return answer
    return answer;
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 4 ];
    N = len(arr);
    M = 3;
     
    print(findLengthOfMaxSubset(arr, N, M));
 
# This code is contributed by AnkitRai01


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find and return the
// length of the longest subset
// whose smallest missing value is M
static int findLengthOfMaxSubset(int []arr,
                                 int n, int m)
{
     
    // Initialize a set
    HashSet s = new HashSet();
 
    int answer = 0;
 
    for(int i = 0; i < n; i++)
    {
        int tmp = arr[i];
 
        // If array element is not
        // equal to M
        if (tmp != m)
        {
             
            // Insert into set
            s.Add(tmp);
 
            // Increment frequency
            answer++;
        }
    }
     
    // Stores minimum missing
    // number
    int min = 1;
 
    // Iterate to find the
    // minimum missing
    // integer
    while (s.Contains(min))
    {
        min++;
    }
 
    // If minimum obtained
    // is less than M
    if (min != m)
    {
         
        // Update answer
        answer = -1;
    }
 
    // Return answer
    return answer;
}
 
// Driver code
public static void Main (string[] args)
{
    int []arr = { 1, 2, 4 };
    int N = arr.Length;
    int M = 3;
     
    Console.Write(findLengthOfMaxSubset(arr, N, M));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(N)

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