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