Largest subset with M as smallest missing number

Given an array arr[] of N positive integers and a positive integer M, the task is to find the length of longest subset whose smallest missing integer is M. If no such subset exists, print “-1”.
Examples: 

Input: arr[] = {1, 2, 4}, M = 3 
Output:
Explanation: 
Possible subsets of the given array are {1}, {2}, {3}, {1, 2}, {2, 4}, {1, 4} and {1, 2, 4}. 
Among these, the subsets containing elements in the range [1, M – 1] but not M is {1, 2} and {1, 2, 4}. 
These subsets contain 3 as the smallest missing positive integer. 
Therefore, subset {1, 2, 4} is the longest required subset with length 3.

Input: arr[] = {2, 2, 3}, M = 4 
Output: -1 
Explanation: 
The smallest missing positive integer in the array is 1. 
Therefore, all possible subsets of the array will have 1 as the smallest missing positive integer. 
Therefore, no subset exists with 4 as the minimum missing integer. 

Naive Approach: The simplest approach is to generate all possible subsets of the given array and store the maximum length among those subsets whose minimum missing integer is M. Finally, print the maximum length as the answer. 

Time Complexity: O(N*2N
Auxiliary Space: O(N)

Efficient Approach: 
To optimize the above approach, consider the following observations. If no element smaller than M is missing in the array, the size of the largest subset will be the 1, consisting of all the array elements excluding M. Otherwise, no subset with the smallest missing number M is possible. 
Follow the steps below: 



  1. Insert all elements of the array except M, into the set.
  2. Find the frequency of elements(say, cnt) in the array arr[], which are not equal to M.
  3. Find the minimum missing number in the set and if it is equal to M, then print the value of cnt as the maximum size of the subset.
  4. Otherwise, print “-1”, as no subset is possible with minimum missing number as M.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement 
// the above approach 
  
#include <bits/stdc++.h> 
#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<int> 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; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

3

Time Complexity: O(N) 
Auxiliary Space: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, offbeat, rutvik_56