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
using System ; using System . Collections . Generic ; class GFG { static int findLengthOfMaxSubset ( int [ ] arr , int n , int m ) { HashSet < int > s = new HashSet < int > ( ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = arr [ i ] ; if ( tmp != m ) { s . Add ( tmp ) ; answer ++ ; } } int min = 1 ; while ( s . Contains ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 4 } ; int N = arr . Length ; int M = 3 ; Console . Write ( findLengthOfMaxSubset ( arr , N , M ) ) ; } }