human
Write me a code to maximum number of pairs of distinct array elements possible by including each element in only one pair and the other steps required given the following:
Maximum number of pairs of distinct array elements possible by including each element in only one pair | Function to count the maximum number of pairs having different element from the given array.
Stores the frequency of array element.
Stores maximum count of pairs.
Increasing the frequency of every element.
Stores the frequencies of array element from highest to lowest.
Pushing the frequencies to the priority queue.
Iterate until size of PQ > 1.
Stores the top two element.
Form the pair between the top two pairs.
Decrement the frequencies.
Insert updated frequencies if it is greater than 0.
Return the total count of resultant pairs.
Driver Code"
Solution in Python:
assistant
#Here is code to maximum number of pairs of distinct array elements possible by including each element in only one pair and the other steps required.
 def maximumPairs ( a , n ) :
	freq = { }
	count = 0
	for i in range ( n ) :
		if a [ i ] in freq :
			freq [ a [ i ] ] += 1
		else :
			freq [ a [ i ] ] = 1
	pq = [ ]
	for key , value in freq . items ( ) :
		pq . append ( value )
	pq . sort ( )
	while ( len ( pq ) > 1 ) :
		freq1 = pq [ len ( pq ) - 1 ]
		pq = pq [ : - 1 ]
		freq2 = pq [ len ( pq ) - 1 ]
		pq = pq [ : - 1 ]
		count += 1
		freq1 -= 1
		freq2 -= 1
		if ( freq1 > 0 ) :
			pq . append ( freq1 )
		if ( freq2 > 0 ) :
			pq . append ( freq2 )
		pq . sort ( )
	return count
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 2 , 4 , 1 , 4 , 3 ]
	N = len ( arr )
	print ( maximumPairs ( arr , N ) )