📜  从数组(a [i],a [j])中选择的随机对具有最大和的概率

📅  最后修改于: 2021-04-21 22:07:19             🧑  作者: Mango

给定的阵列ARR [] N个整数的,任务是找到获得最大的总和对的概率(ARR [I],编曲[j]的)时,选择的随机对从阵列。
例子:

方法:运行两个嵌套循环以获取每个单对的总和,保留任何一对的最大总和及其计数(即得出该总和的对数)。现在,获得此总和的概率为(count / totalPairs) ,其中totalPairs =(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


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 curren
            # 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


Javascript


输出:
0.2

时间复杂度: O(n 2 )