Counting total distinct elements
int dist = n + 1 ; for ( int i = 0 ; i < frq . Count ; i ++ ) { if ( ( int ) frq [ i ] == 0 ) { dist -- ; } } int ans1 = Math . Min ( maxfrq - 1 , dist ) ; int ans2 = Math . Min ( maxfrq , dist - 1 ) ;