Minimum number of distinct elements after removing M items | Set 2 | C ++ program for the above approach ; Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver Code ; Initialize array ; Size of array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctNumbers ( int arr [ ] , int m , int n ) { unordered_map < int , int > count ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; vector < int > fre_arr ( n + 1 , 0 ) ; for ( auto it : count ) { fre_arr [ it . second ] ++ ; } int ans = count . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 2 ; cout << distinctNumbers ( arr , m , n ) ; return 0 ; }