Largest subset with M as smallest missing number | 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
def findLengthOfMaxSubset ( arr , n , m ) : NEW_LINE INDENT s = [ ] ; NEW_LINE answer = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp = arr [ i ] ; NEW_LINE if ( tmp != m ) : NEW_LINE INDENT s . append ( tmp ) ; NEW_LINE answer += 1 ; NEW_LINE DEDENT DEDENT min = 1 ; NEW_LINE while ( s . count ( min ) ) : NEW_LINE INDENT min += 1 ; NEW_LINE DEDENT if ( min != m ) : NEW_LINE INDENT answer = - 1 ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE M = 3 ; NEW_LINE print ( findLengthOfMaxSubset ( arr , N , M ) ) ; NEW_LINE DEDENT