Maximum number of elements that can be removed such that MEX of the given array remains unchanged | Java program for the above approach ; 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
import java . io . * ; import java . util . * ; class GFG { static void countRemovableElem ( int [ ] arr , int N ) { int [ ] hash = new int [ N + 1 ] ; Arrays . fill ( hash , 0 ) ; int mex = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= N ) hash [ arr [ i ] ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] == 0 ) { mex = i ; break ; } } System . out . println ( N - ( mex - 1 ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 6 } ; int N = arr . length ; countRemovableElem ( arr , N ) ; } }