Maximum Count of pairs having equal Sum based on the given conditions | Function to find the maximum count of pairs having equal sum ; Size of the array ; Iterate through evey sum of pairs possible from the given array ; Count of pairs with given sum ; Check for a possible pair ; Update count of possible pair ; Update the answer by taking the pair which is maximum for every possible sum ; Return the max possible pair ; Function to return the count of pairs ; Size of the array ; Stores the frequencies ; Count the frequency ; Driver Code
def maxCount ( freq , maxi , mini ) : NEW_LINE INDENT n = len ( freq ) - 1 NEW_LINE ans = 0 NEW_LINE sum = 2 * mini NEW_LINE while sum <= 2 * maxi : NEW_LINE INDENT possiblePair = 0 NEW_LINE for firElement in range ( 1 , ( sum + 1 ) // 2 ) : NEW_LINE INDENT if ( sum - firElement <= maxi ) : NEW_LINE INDENT possiblePair += min ( freq [ firElement ] , freq [ sum - firElement ] ) NEW_LINE DEDENT DEDENT sum += 1 NEW_LINE if ( sum % 2 == 0 ) : NEW_LINE INDENT possiblePair += freq [ sum // 2 ] // 2 NEW_LINE DEDENT ans = max ( ans , possiblePair ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def countofPairs ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE freq = [ 0 ] * ( n + 1 ) NEW_LINE maxi = - 1 NEW_LINE mini = n + 1 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT maxi = max ( maxi , a [ i ] ) NEW_LINE mini = min ( mini , a [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT return maxCount ( freq , maxi , mini ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ] NEW_LINE print ( countofPairs ( a ) ) NEW_LINE DEDENT