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
function maxCount ( freq , maxi , mini ) { let n = freq . length - 1 ; let ans = 0 ; for ( let sum = 2 * mini ; sum <= 2 * maxi ; ++ sum ) { let possiblePair = 0 ; for ( let firElement = 1 ; firElement < Math . floor ( ( sum + 1 ) / 2 ) ; firElement ++ ) { if ( sum - firElement <= maxi ) { possiblePair += Math . min ( freq [ firElement ] , freq [ sum - firElement ] ) ; } } if ( sum % 2 == 0 ) { possiblePair += freq [ sum / 2 ] / 2 ; } ans = Math . max ( ans , possiblePair ) ; } return ans ; } function countofPairs ( a ) { let n = a . length ; let freq = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let maxi = - 1 ; let mini = n + 1 ; for ( let i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; mini = Math . min ( mini , a [ i ] ) ; } for ( let i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; return maxCount ( freq , maxi , mini ) ; } let a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ] ; document . write ( countofPairs ( a ) ) ;