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 ; Prlet the maximum numbers that can be removed ; Given array ; Size of the array ; Function Call
function countRemovableElem ( arr , N ) { let hash = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { hash [ i ] = 0 ; } let mex = N + 1 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= N ) hash [ arr [ i ] ] = 1 ; } for ( let i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] == 0 ) { mex = i ; break ; } } document . write ( N - ( mex - 1 ) ) ; } let arr = [ 2 , 3 , 5 , 1 , 6 ] ; let N = arr . length ; countRemovableElem ( arr , N ) ;