Subsequence pair from given Array having all unique and all same elements respectively | C ++ program for the above approach ; Function to find the maximum length of subsequences with given property ; To store the frequency ; Traverse the array to store the frequency ; M . size ( ) given count of distinct element in arr [ ] ; Traverse map to find max frequency ; Find the maximum length on the basis of two cases in the approach ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSubsequence ( int arr [ ] , int N ) { unordered_map < int , int > M ; for ( int i = 0 ; i < N ; i ++ ) { M [ arr [ i ] ] ++ ; } int distinct_size = M . size ( ) ; int maxFreq = 1 ; for ( auto & it : M ) { maxFreq = max ( maxFreq , it . second ) ; } cout << max ( min ( distinct_size , maxFreq - 1 ) , min ( distinct_size - 1 , maxFreq ) ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSubsequence ( arr , N ) ; return 0 ; }