Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | C ++ Program to implement the above approach ; Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxUniqueElements ( int A [ ] , int N ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ A [ i ] ] ++ ; } int cnt = 0 ; for ( auto x : mp ) { if ( x . second % 2 == 0 ) { cnt ++ ; } } int ans = mp . size ( ) ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } int main ( ) { int N = 5 ; int A [ ] = { 1 , 2 , 1 , 3 , 7 } ; cout << maxUniqueElements ( A , N ) ; }