Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum

Given an array arr[] of N integers, the task is to find the probability of getting the maximum sum pair (arr[i], arr[j]) from the array when a random pair is chosen.
Examples:Â
Â
Input: arr[] = {3, 3, 3, 3}Â
Output: 1Â
All the pairs will give the maximum sum i.e. 6.
Input: arr[] = {1, 1, 1, 2, 2, 2}Â
Output: 0.2Â
Only the pairs (2, 2), (2, 2) and (2, 2) will giveÂ
the maximum sum out of 15 pairs.Â
3 / 15 = 0.2Â
Â
Â
Approach: Run two nested loops to get the sum for every single pair, keep the maximum sum for any pair and its count (i.e. the number of pairs that give this sum). Now, the probability of getting this sum will be (count / totalPairs) where totalPairs = (n * (n – 1)) / 2.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the probability// of getting the maximum pair sum// when a random pair is chosen// from the given arrayfloat findProb(int arr[], int n){Â
    // Initialize the maximum sum, its count    // and the count of total pairs    long maxSum = INT_MIN, maxCount = 0, totalPairs = 0;Â
    // For every single pair    for (int i = 0; i < n - 1; i++) {        for (int j = i + 1; j < n; j++) {Â
            // Get the sum of the current pair            int sum = arr[i] + arr[j];Â
            // If the sum is equal to the current            // maximum sum so far            if (sum == maxSum) {Â
                // Increment its count                maxCount++;            }Â
            // If the sum is greater than            // the current maximum            else if (sum > maxSum) {Â
                // Update the current maximum and                // re-initialize the count to 1                maxSum = sum;                maxCount = 1;            }Â
            totalPairs++;        }    }Â
    // Find the required probability    float prob = (float)maxCount / (float)totalPairs;    return prob;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 1, 1, 2, 2, 2 };Â Â Â Â int n = sizeof(arr) / sizeof(int);Â
    cout << findProb(arr, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â
// Function to return the probability// of getting the maximum pair sum// when a random pair is chosen// from the given arraystatic float findProb(int arr[], int n){Â
    // Initialize the maximum sum, its count    // and the count of total pairs    long maxSum = Integer.MIN_VALUE,         maxCount = 0, totalPairs = 0;Â
    // For every single pair    for (int i = 0; i < n - 1; i++)     {        for (int j = i + 1; j < n; j++)         {Â
            // Get the sum of the current pair            int sum = arr[i] + arr[j];Â
            // If the sum is equal to the current            // maximum sum so far            if (sum == maxSum)            {Â
                // Increment its count                maxCount++;            }Â
            // If the sum is greater than            // the current maximum            else if (sum > maxSum)             {Â
                // Update the current maximum and                // re-initialize the count to 1                maxSum = sum;                maxCount = 1;            }Â
            totalPairs++;        }    }Â
    // Find the required probability    float prob = (float)maxCount /                  (float)totalPairs;    return prob;}Â
// Driver codepublic static void main(String args[]){Â Â Â Â int arr[] = { 1, 1, 1, 2, 2, 2 };Â Â Â Â int n = arr.length;Â
    System.out.println(findProb(arr, n));}}Â
// This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach import sysÂ
# Function to return the probability # of getting the maximum pair sum # when a random pair is chosen # from the given array def findProb(arr, n) :Â
    # Initialize the maximum sum, its count     # and the count of total pairs     maxSum = -(sys.maxsize - 1);    maxCount = 0;    totalPairs = 0; Â
    # For every single pair     for i in range(n - 1) :        for j in range(i + 1, n) :                         # Get the sum of the current pair             sum = arr[i] + arr[j];                         # If the sum is equal to the current            # maximum sum so far            if (sum == maxSum) :                                 # Increment its count                maxCount += 1; Â
            # If the sum is greater than             # the current maximum             elif (sum > maxSum) :Â
                # Update the current maximum and                 # re-initialize the count to 1                 maxSum = sum;                 maxCount = 1; Â
            totalPairs += 1; Â
    # Find the required probability     prob = maxCount / totalPairs;          return prob; Â
# Driver code if __name__ == "__main__" : Â
    arr = [ 1, 1, 1, 2, 2, 2 ];     n = len(arr);         print(findProb(arr, n)); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of above approachusing System;Â Â Â Â Â class GFG{Â
// Function to return the probability// of getting the maximum pair sum// when a random pair is chosen// from the given arraystatic float findProb(int []arr, int n){Â
    // Initialize the maximum sum, its count    // and the count of total pairs    long maxSum = int.MinValue,        maxCount = 0, totalPairs = 0;Â
    // For every single pair    for (int i = 0; i < n - 1; i++)     {        for (int j = i + 1; j < n; j++)         {Â
            // Get the sum of the current pair            int sum = arr[i] + arr[j];Â
            // If the sum is equal to the current            // maximum sum so far            if (sum == maxSum)            {Â
                // Increment its count                maxCount++;            }Â
            // If the sum is greater than            // the current maximum            else if (sum > maxSum)             {Â
                // Update the current maximum and                // re-initialize the count to 1                maxSum = sum;                maxCount = 1;            }Â
            totalPairs++;        }    }Â
    // Find the required probability    float prob = (float)maxCount /                  (float)totalPairs;    return prob;}Â
// Driver codepublic static void Main(String []args){Â Â Â Â int []arr = { 1, 1, 1, 2, 2, 2 };Â Â Â Â int n = arr.Length;Â
    Console.WriteLine(findProb(arr, n));}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the probability// of getting the maximum pair sum// when a random pair is chosen// from the given arrayfunction findProb(arr, n){Â
    // Initialize the maximum sum, its count    // and the count of total pairs    var maxSum = -100000000, maxCount = 0, totalPairs = 0;Â
    // For every single pair    for (var i = 0; i < n - 1; i++) {        for (var j = i + 1; j < n; j++) {Â
            // Get the sum of the current pair            var sum = arr[i] + arr[j];Â
            // If the sum is equal to the current            // maximum sum so far            if (sum == maxSum) {Â
                // Increment its count                maxCount++;            }Â
            // If the sum is greater than            // the current maximum            else if (sum > maxSum) {Â
                // Update the current maximum and                // re-initialize the count to 1                maxSum = sum;                maxCount = 1;            }Â
            totalPairs++;        }    }Â
    // Find the required probability    var prob = maxCount / totalPairs;    return prob;}Â
// Driver codevar arr = [ 1, 1, 1, 2, 2, 2 ]var n = arr.length;document.write(findProb(arr, n));Â
// This code is contributed by rutvik_56.</script> |
0.2
Â
Time Complexity: O(N2)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


