Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array
Last Updated :
26 Jul, 2021
Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to find the probability of randomly selecting the two numbers from arr1[] and arr2[] respectively, such that the first selected element is strictly less than the second selected element.
Examples:
Input: arr1[] = {3, 2, 1, 1}, arr2[] = {1, 2, 5, 2}
Output: 0.5
Explanation:
Following are the ways of selecting the array elements from both the arrays first number is less than the second number:
- Selecting arr1[0], there are 1 way of selecting an element in arr2[].
- Selecting arr1[1], there are 1 way of selecting an element in arr2[].
- Selecting arr1[2], there are 3 way of selecting an element in arr2[].
- Selecting arr1[3], there are 3 way of selecting an element in arr2[]
Therefore, there are totals of (3 + 3 + 1 + 1 = 8) ways of selecting the elements from both arrays satisfying the conditions. Hence, the probability is (8/(4*4)) = 0.5.
Input: arr1[] = {5, 2, 6, 1}, arr2[] = {1, 6, 10, 1}
Output: 0.4375
Naive Approach: The given problem can be solved based on the following observations:
- The idea is to use the concept of conditional probability. The probability of selecting an element from array arr1[] is 1/N.
- Now suppose X is the count of elements in arr2[] greater than the selected elements of arr1[] then the probability of selecting one such element from arr2[] is X/M.
- Therefore, the probability of selecting two elements such that the first element is less than the second selected element is the sum of (1/N)*(X/M) for every element in arr1[].
Follow the steps below to solve the problem:
- Initialize a variable say, res as 0 to stores the resultant probability.
- Traverse the given the array arr1[] and perform the following steps:
- After completing the above steps, print the value of res as the resultant probability.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double probability(vector< int > arr1,vector< int > arr2)
{
int N = arr1.size();
int M = arr2.size();
double res = 0;
for ( int i = 0; i < N; i++) {
int y = 0;
for ( int j = 0; j < M; j++) {
if (arr2[j] > arr1[i])
y++;
}
res += y;
}
res = ( double )res / ( double )(N * M);
return res;
}
int main()
{
vector< int > arr1 = { 5, 2, 6, 1 };
vector< int > arr2 = { 1, 6, 10, 1 };
cout<<probability(arr1, arr2);
}
|
Java
import java.util.*;
class GFG {
static double probability( int [] arr1,
int [] arr2)
{
int N = arr1.length;
int M = arr2.length;
double res = 0 ;
for ( int i = 0 ; i < N; i++) {
int y = 0 ;
for ( int j = 0 ; j < M; j++) {
if (arr2[j] > arr1[i])
y++;
}
res += y;
}
res = ( double )res / ( double )(N * M);
return res;
}
public static void main(String[] args)
{
int [] arr1 = { 5 , 2 , 6 , 1 };
int [] arr2 = { 1 , 6 , 10 , 1 };
System.out.println(
probability(arr1, arr2));
}
}
|
Python3
def probability(arr1, arr2):
N = len (arr1)
M = len (arr2)
res = 0
for i in range (N):
y = 0
for j in range (M):
if (arr2[j] > arr1[i]):
y + = 1
res + = y
res = res / (N * M)
return res
if __name__ = = "__main__" :
arr1 = [ 5 , 2 , 6 , 1 ]
arr2 = [ 1 , 6 , 10 , 1 ]
print (probability(arr1, arr2))
|
C#
using System;
class GFG {
static double probability( int [] arr1, int [] arr2)
{
int N = arr1.Length;
int M = arr2.Length;
double res = 0;
for ( int i = 0; i < N; i++) {
int y = 0;
for ( int j = 0; j < M; j++) {
if (arr2[j] > arr1[i])
y++;
}
res += y;
}
res = ( double )res / ( double )(N * M);
return res;
}
static void Main()
{
int [] arr1 = { 5, 2, 6, 1 };
int [] arr2 = { 1, 6, 10, 1 };
Console.WriteLine(probability(arr1, arr2));
}
}
|
Javascript
<script>
function probability(arr1, arr2)
{
let N = arr1.length;
let M = arr2.length;
let res = 0;
for (let i = 0; i < N; i++) {
let y = 0;
for (let j = 0; j < M; j++) {
if (arr2[j] > arr1[i])
y++;
}
res += y;
}
res = (res / (N * M));
return res;
}
let arr1 = [ 5, 2, 6, 1 ];
let arr2 = [ 1, 6, 10, 1 ];
document.write(
probability(arr1, arr2));
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using Binary Search. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countGreater( int * arr, int k);
float probability( int * arr1,
int * arr2)
{
int N = 4;
int M = 4;
float res = 0;
sort(arr2, arr2 + M);
for ( int i = 0; i < N; i++) {
int y = countGreater(
arr2, arr1[i]);
res += y;
}
res = res / (N * M);
return res;
}
int countGreater( int * arr,
int k)
{
int n = 4;
int l = 0;
int r = n - 1;
int leftGreater = n;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] > k) {
leftGreater = m;
r = m - 1;
}
else
l = m + 1;
}
return (n - leftGreater);
}
int main()
{
int arr1[] = { 5, 2, 6, 1 };
int arr2[] = { 1, 6, 10, 1 };
cout << probability(arr1, arr2);
return 0;
}
|
Java
import java.util.*;
class GFG {
static double probability( int [] arr1,
int [] arr2)
{
int N = arr1.length;
int M = arr2.length;
double res = 0 ;
Arrays.sort(arr2);
for ( int i = 0 ; i < N; i++) {
int y = countGreater(
arr2, arr1[i]);
res += y;
}
res = ( double )res / ( double )(N * M);
return res;
}
static int countGreater( int [] arr,
int k)
{
int n = arr.length;
int l = 0 ;
int r = n - 1 ;
int leftGreater = n;
while (l <= r) {
int m = l + (r - l) / 2 ;
if (arr[m] > k) {
leftGreater = m;
r = m - 1 ;
}
else
l = m + 1 ;
}
return (n - leftGreater);
}
public static void main(String[] args)
{
int [] arr1 = { 5 , 2 , 6 , 1 };
int [] arr2 = { 1 , 6 , 10 , 1 };
System.out.println(
probability(arr1, arr2));
}
}
|
Python3
def probability(arr1, arr2):
n = len (arr1)
m = len (arr2)
res = 0
arr2.sort()
for i in range (n):
y = countGreater(arr2, arr1[i])
res + = y
res / = (n * m)
return res
def countGreater(arr, k):
n = len (arr)
l = 0
r = n - 1
leftGreater = n
while l < = r:
m = (l + r) / / 2
if (arr[m] > k):
leftGreater = m
r = m - 1
else :
l = m + 1
return n - leftGreater
if __name__ = = '__main__' :
arr1 = [ 5 , 2 , 6 , 1 ]
arr2 = [ 1 , 6 , 10 , 1 ]
print (probability(arr1, arr2))
|
C#
using System;
class GFG {
static double probability( int [] arr1,
int [] arr2)
{
int N = arr1.Length;
int M = arr2.Length;
double res = 0;
Array.Sort(arr2);
for ( int i = 0; i < N; i++) {
int y = countGreater(
arr2, arr1[i]);
res += y;
}
res = ( double )res / ( double )(N * M);
return res;
}
static int countGreater( int [] arr,
int k)
{
int n = arr.Length;
int l = 0;
int r = n - 1;
int leftGreater = n;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] > k) {
leftGreater = m;
r = m - 1;
}
else
l = m + 1;
}
return (n - leftGreater);
}
public static void Main()
{
int [] arr1 = { 5, 2, 6, 1 };
int [] arr2 = { 1, 6, 10, 1 };
Console.Write(
probability(arr1, arr2));
}
}
|
Javascript
<script>
function probability(arr1, arr2)
{
var N = 4;
var M = 4;
var res = 0;
arr2.sort( function (a, b) {
return a - b;
});
for ( var i = 0; i < N; i++) {
var y = countGreater( arr2, arr1[i]);
res += y;
}
res = res / (N * M);
return res;
}
function countGreater(arr, k)
{
var n = 4;
var l = 0;
var r = n - 1;
var leftGreater = n;
while (l <= r) {
var m = Math.floor(l + (r - l) / 2);
if (arr[m] > k) {
leftGreater = m;
r = m - 1;
}
else
l = m + 1;
}
return n - leftGreater;
}
var arr1 = [ 5, 2, 6, 1 ];
var arr2 = [ 1, 6, 10, 1 ];
document.write(probability(arr1, arr2));
</script>
|
Time Complexity: O(N * log M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...