Check if an array can be reduced to at most length K by removal of distinct elements | C ++ program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxCount ( int arr [ ] , int N , int K ) { set < int > st ; for ( int i = 0 ; i < N ; i ++ ) { st . insert ( arr [ i ] ) ; } if ( N - st . size ( ) <= K ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxCount ( arr , N , K ) ; return 0 ; }