VOOZH about

URL: https://www.geeksforgeeks.org/dsa/probability-random-pair-maximum-weighted-pair/

⇱ Probability of a random pair being the maximum weighted pair - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Probability of a random pair being the maximum weighted pair

Last Updated : 27 Jul, 2022

Given two arrays A and B, a random pair is picked having an element from array A and another from array B. Output the probability of the pair being maximum weighted.

Examples:  

Input : A[] = 1 2 3
 B[] = 1 3 3
Output : 0.222
Explanation : Possible pairs are : {1, 1}, 
{1, 3}, {1, 3}, {2, 1}, {2, 3}, {2, 3},
{3, 1}, {3, 3}, {3, 3} i.e. 9.
The pair with maximum weight is {3, 3} with
frequency 2. So, the probability of random 
pair being maximum is 2/9 = 0.2222.
  • Brute Force Method : Generate all possible pairs in N^2 time complexity and count 
    maximum weighted pairs.
  • Better Method : Sort both the arrays and count the last (max) elements from A and B. No. of maximum weighted pairs will be product of both counts. The probability will be 
    (product of counts) / sizeof(A) * sizeof(B) 
  • Best Method Best approach will be to traverse both the arrays and count the maximum element. No. of maximum weighted pairs will be product of both counts. The probability will be (product of counts) / sizeof(A) * sizeof(B) 

Below is the implementation:  


Output
0.222222

Time Complexity: O(n).
Auxiliary Space: O(1).

Comment
Article Tags: