原文:

给定一个由 n 个整数组成的数组 arr[] ,任务是在选择一个随机对时,找出从数组中获得最大和对 (arr[i],arr[j]) 的概率。 举例:

输入: arr[] = {3,3,3,3} 输出: 1 所有对将给出最大和,即 6。 输入: arr[] = {1,1,1,2,2,2} 输出: 0.2 只有对(2,2)、(2,2)和(2,2)会给出 15 对中的最大和。 3 / 15 = 0.2

方法:运行两个嵌套循环以获得每一对的总和,保留任何对的最大总和及其计数(即给出该总和的对的数量)。现在,得到这个总数的概率将是(计数/总计对),其中总计对=(n *(n–1))/2。 以下是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// function to return the probability
// of getting the maximum pair sum
// when a random pair is chosen
// from the given array
float 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 code
int 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 approach
import java.util.*;
class gfg
{
// function to return the probability
// of getting the maximum pair sum
// when a random pair is chosen
// from the given array
static 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 code
public 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

python 3

# 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 approach
using system;
class gfg
{
// function to return the probability
// of getting the maximum pair sum
// when a random pair is chosen
// from the given array
static 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 code
public 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

java 描述语言


output: 

0.2

时间复杂度: o(n 2 )