Probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | Python3 implementation of the approach ; Function to return the probability of getting the maximum pair sum when a random pair is chosen from the given array ; Initialize the maximum sum , its count and the count of total pairs ; For every single pair ; Get the sum of the current pair ; If the sum is equal to the current maximum sum so far ; Increment its count ; If the sum is greater than the current maximum ; Update the current maximum and re - initialize the count to 1 ; Find the required probability ; Driver code
import sys NEW_LINE def findProb ( arr , n ) : NEW_LINE INDENT maxSum = - ( sys . maxsize - 1 ) ; NEW_LINE maxCount = 0 ; NEW_LINE totalPairs = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = arr [ i ] + arr [ j ] ; NEW_LINE if ( sum == maxSum ) : NEW_LINE INDENT maxCount += 1 ; NEW_LINE DEDENT elif ( sum > maxSum ) : NEW_LINE INDENT maxSum = sum ; NEW_LINE maxCount = 1 ; NEW_LINE DEDENT totalPairs += 1 ; NEW_LINE DEDENT DEDENT prob = maxCount / totalPairs ; NEW_LINE return prob ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 , 2 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findProb ( arr , n ) ) ; NEW_LINE DEDENT