Construct MEX array from the given array | Python3 program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Prthe array B ; Driver Code ; Given array ; Size of array ; Function call
MAXN = 100001 NEW_LINE def constructMEX ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * MAXN NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] = 1 NEW_LINE DEDENT MexOfArr = 0 NEW_LINE for i in range ( 1 , MAXN ) : NEW_LINE INDENT if ( hash [ i ] == 0 ) : NEW_LINE INDENT MexOfArr = i NEW_LINE break NEW_LINE DEDENT DEDENT B = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < MexOfArr ) : NEW_LINE INDENT B [ i ] = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT B [ i ] = MexOfArr NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE constructMEX ( arr , N ) NEW_LINE DEDENT