Maximum number of elements that can be removed such that MEX of the given array remains unchanged | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countRemovableElem ( int arr [ ] , int N ) { int hash [ N + 1 ] = { 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 ; } } cout << N - ( mex - 1 ) ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 1 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countRemovableElem ( arr , N ) ; return 0 ; }