Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | 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
def maxUniqueElements ( A , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if A [ i ] in mp : NEW_LINE INDENT mp [ A [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ A [ i ] ] = 1 NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT if ( value % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ans = len ( mp ) NEW_LINE if ( cnt % 2 == 1 ) : NEW_LINE INDENT ans -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE A = [ 1 , 2 , 1 , 3 , 7 ] NEW_LINE print ( maxUniqueElements ( A , N ) ) NEW_LINE DEDENT