Maximum number of elements that can be removed such that MEX of the given array remains unchanged | Function to find the maximum number of elements that can be removed ; Initialize hash [ ] with 0 s ; Initialize MEX ; Set hash [ i ] = 1 , if i is present in arr [ ] ; Find MEX from the hash ; Print the maximum numbers that can be removed ; Driver Code ; Given array ; Size of the array ; Function Call
def countRemovableElem ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * ( N + 1 ) NEW_LINE mex = N + 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] <= N ) : NEW_LINE INDENT hash [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( hash [ i ] == 0 ) : NEW_LINE INDENT mex = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( N - ( mex - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 1 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE countRemovableElem ( arr , N ) NEW_LINE DEDENT