Given an array of N integers, You have to find the probability of choosing a random pair(i, j), i < j such that A[i] + A[j] is maximum.
Examples :
Input : A[] = {3, 3, 3, 3}
Output : 1.0000
Explanation :
Here, maximum sum we can get by selecting
any pair is 6.
Total number of pairs possible = 6.
Pairs with maximum sum = 6.
Probability = 6/6 = 1.0000
Input : A[] = {1, 2, 2, 3}
Output : 0.3333
Explanation :
Here, maximum sum we can get by selecting
a pair is 5.
Total number of pairs possible = 6.
Pairs with maximum sum = {2, 3} and {2, 3} = 2.
Probability = 2/6 = 0.3333
Probability(event) = Number of favorable outcomes /
Total number of outcomes
Naive approach : We can solve this problem using brute force solution overall pair (i, j), i < j to get the maximum value possible and then again do a brute force to calculate the number of times the maximum is attained.
Efficient Approach : Observe that we get maximum pair sum only when the pairs consists of first and second maximum elements of the array. So, the problem is to calculate the number of occurrences of those elements and calculate the favorable outcomes using a formula.
Favorable outcomes = f2 (frequency of second maximum
element(f2), if maximum element occurs only once).
or
Favorable outcomes = f1 * (f1 - 1) / 2,
(when frequency of maximum element(f1)
is greater than 1).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countMaxSumPairs( int a[], int n)
{
int first = INT_MIN, second = INT_MIN;
for ( int i = 0; i < n; i++) {
if (a[i] > first) {
second = first;
first = a[i];
}
else if (a[i] > second && a[i] != first)
second = a[i];
}
int cnt1 = 0, cnt2 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == first)
cnt1++;
if (a[i] == second)
cnt2++;
}
if (cnt1 == 1)
return cnt2;
if (cnt1 > 1)
return cnt1 * (cnt1 - 1) / 2;
}
float findMaxSumProbability( int a[], int n)
{
int total = n * (n - 1) / 2;
int max_sum_pairs = countMaxSumPairs(a, n);
return ( float )max_sum_pairs/( float )total;
}
int main()
{
int a[] = { 1, 2, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << findMaxSumProbability(a, n);
return 0;
}
|
Java
import java.util.Scanner;
import java.io.*;
class GFG {
static int countMaxSumPairs( int a[], int n)
{
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++) {
if (a[i] > first)
{
second = first;
first = a[i];
}
else if (a[i] > second && a[i] != first)
second = a[i];
}
int cnt1 = 0 , cnt2 = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == first)
cnt1++;
if (a[i] == second)
cnt2++;
}
if (cnt1 == 1 )
return cnt2;
if (cnt1 > 1 )
return cnt1 * (cnt1 - 1 ) / 2 ;
return 0 ;
}
static float findMaxSumProbability( int a[], int n)
{
int total = n * (n - 1 ) / 2 ;
int max_sum_pairs = countMaxSumPairs(a, n);
return ( float )max_sum_pairs/( float )total;
}
public static void main (String[] args) {
int a[] = { 1 , 2 , 2 , 3 };
int n = a.length;;
System.out.println(findMaxSumProbability(a, n));
}
}
|
Python 3
def countMaxSumPairs(a, n):
first = 0
second = 0
for i in range (n):
if (a[i] > first) :
second = first
first = a[i]
elif (a[i] > second and a[i] ! = first):
second = a[i]
cnt1 = 0
cnt2 = 0
for i in range (n):
if (a[i] = = first):
cnt1 + = 1
if (a[i] = = second):
cnt2 + = 1
if (cnt1 = = 1 ) :
return cnt2
if (cnt1 > 1 ) :
return cnt1 * (cnt1 - 1 ) / 2
def findMaxSumProbability(a, n):
total = n * (n - 1 ) / 2
max_sum_pairs = countMaxSumPairs(a, n)
return max_sum_pairs / total
if __name__ = = "__main__" :
a = [ 1 , 2 , 2 , 3 ]
n = len (a)
print (findMaxSumProbability(a, n))
|
C#
using System;
public class GFG{
static int countMaxSumPairs( int []a, int n)
{
int first = int .MinValue, second = int .MinValue;
for ( int i = 0; i < n; i++) {
if (a[i] > first)
{
second = first;
first = a[i];
}
else if (a[i] > second && a[i] != first)
second = a[i];
}
int cnt1 = 0, cnt2 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == first)
cnt1++;
if (a[i] == second)
cnt2++;
}
if (cnt1 == 1)
return cnt2;
if (cnt1 > 1)
return cnt1 * (cnt1 - 1) / 2;
return 0;
}
static float findMaxSumProbability( int []a, int n)
{
int total = n * (n - 1) / 2;
int max_sum_pairs = countMaxSumPairs(a, n);
return ( float )max_sum_pairs/( float )total;
}
static public void Main ()
{
int []a = { 1, 2, 2, 3 };
int n = a.Length;;
Console.WriteLine(findMaxSumProbability(a, n));
}
}
|
PHP
<?php
function countMaxSumPairs( $a , $n )
{
$first = PHP_INT_MIN;
$second = PHP_INT_MIN;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $first )
{
$second = $first ;
$first = $a [ $i ];
}
else if ( $a [ $i ] > $second &&
$a [ $i ] != $first )
$second = $a [ $i ];
}
$cnt1 = 0;
$cnt2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == $first )
$cnt1 ++;
if ( $a [ $i ] == $second )
$cnt2 ++;
}
if ( $cnt1 == 1)
return $cnt2 ;
if ( $cnt1 > 1)
return $cnt1 * ( $cnt1 - 1) / 2;
}
function findMaxSumProbability( $a , $n )
{
$total = $n * ( $n - 1) / 2;
$max_sum_pairs = countMaxSumPairs( $a , $n );
return (float) $max_sum_pairs / (float) $total ;
}
$a = array (1, 2, 2, 3 );
$n = sizeof( $a );
echo findMaxSumProbability( $a , $n );
?>
|
Javascript
<script>
function countMaxSumPairs(a, n)
{
let first = Number.MIN_VALUE,
second = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
{
if (a[i] > first)
{
second = first;
first = a[i];
}
else if (a[i] > second && a[i] != first)
second = a[i];
}
let cnt1 = 0, cnt2 = 0;
for (let i = 0; i < n; i++) {
if (a[i] == first)
cnt1++;
if (a[i] == second)
cnt2++;
}
if (cnt1 == 1)
return cnt2;
if (cnt1 > 1)
return cnt1 * (cnt1 - 1) / 2;
return 0;
}
function findMaxSumProbability(a, n)
{
let total = n * (n - 1) / 2;
let max_sum_pairs = countMaxSumPairs(a, n);
return max_sum_pairs/total;
}
let a = [ 1, 2, 2, 3 ];
let n = a.length;;
document.write(findMaxSumProbability(a, n));
</script>
|
Time complexity: O(n) where n is the size of the given array.
Auxiliary space: O(1)