text: Probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | C ++ 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; float findProb ( int arr [ ] , int n ) { long maxSum = INT_MIN , maxCount = 0 , totalPairs = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( sum == maxSum ) { maxCount ++ ; } else if ( sum > maxSum ) { maxSum = sum ; maxCount = 1 ; } totalPairs ++ ; } } float prob = ( float ) maxCount / ( float ) totalPairs ; return prob ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findProb ( arr , n ) ; return 0 ; }