# Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array

• Difficulty Level : Easy
• 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:

1. Selecting arr1, there are 1 way of selecting an element in arr2[].
2. Selecting arr1, there are 1 way of selecting an element in arr2[].
3. Selecting arr1, there are 3 way of selecting an element in arr2[].
4. Selecting arr1, 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:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find  probability``// such that x < y and X belongs to``// arr1[] and Y belongs to arr2[]``double` `probability(vector<``int``> arr1,vector<``int``> arr2)``{``    ``// Stores the length of arr1``    ``int` `N = arr1.size();` `    ``// Stores the length of arr2``    ``int` `M = arr2.size();` `    ``// Stores the result``    ``double` `res = 0;` `    ``// Traverse the arr1[]``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Stores the count of``        ``// elements in arr2 that``        ``// are greater than arr[i]``        ``int` `y = 0;` `        ``// Traverse the arr2[]``        ``for` `(``int` `j = 0; j < M; j++) {` `            ``// If arr2[j] is greater``            ``// than arr1[i]``            ``if` `(arr2[j] > arr1[i])``                ``y++;``        ``}` `        ``// Increment res by y``        ``res += y;``    ``}` `    ``// Update the value of res``    ``res = (``double``)res / (``double``)(N * M);` `    ``// Return resultant probability``    ``return` `res;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr1 = { 5, 2, 6, 1 };``    ``vector<``int``> arr2 = { 1, 6, 10, 1 };``    ``cout<

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `    ``// Function to find  probability``    ``// such that x < y and X belongs to``    ``// arr1[] and Y belongs to arr2[]``    ``static` `double` `probability(``int``[] arr1,``                              ``int``[] arr2)``    ``{``        ``// Stores the length of arr1``        ``int` `N = arr1.length;` `        ``// Stores the length of arr2``        ``int` `M = arr2.length;` `        ``// Stores the result``        ``double` `res = ``0``;` `        ``// Traverse the arr1[]``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Stores the count of``            ``// elements in arr2 that``            ``// are greater than arr[i]``            ``int` `y = ``0``;` `            ``// Traverse the arr2[]``            ``for` `(``int` `j = ``0``; j < M; j++) {` `                ``// If arr2[j] is greater``                ``// than arr1[i]``                ``if` `(arr2[j] > arr1[i])``                    ``y++;``            ``}` `            ``// Increment res by y``            ``res += y;``        ``}` `        ``// Update the value of res``        ``res = (``double``)res / (``double``)(N * M);` `        ``// Return resultant probability``        ``return` `res;``    ``}` `    ``// Driver Code``    ``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

 `# Python 3 program for the above approach` `# Function to find  probability``# such that x < y and X belongs to``# arr1[] and Y belongs to arr2[]``def` `probability(arr1, arr2):` `    ``# Stores the length of arr1``    ``N ``=` `len``(arr1)` `    ``# Stores the length of arr2``    ``M ``=` `len``(arr2)` `    ``# Stores the result``    ``res ``=` `0` `    ``# Traverse the arr1[]``    ``for` `i ``in` `range``(N):` `        ``# Stores the count of``        ``# elements in arr2 that``        ``# are greater than arr[i]``        ``y ``=` `0` `        ``# Traverse the arr2[]``        ``for` `j ``in` `range``(M):` `            ``# If arr2[j] is greater``            ``# than arr1[i]``            ``if` `(arr2[j] > arr1[i]):``                ``y ``+``=` `1` `        ``# Increment res by y``        ``res ``+``=` `y` `    ``# Update the value of res``    ``res ``=` `res ``/` `(N ``*` `M)` `    ``# Return resultant probability``    ``return` `res`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr1 ``=` `[``5``, ``2``, ``6``, ``1``]``    ``arr2 ``=` `[``1``, ``6``, ``10``, ``1``]``    ``print``(probability(arr1, arr2))` `    ``# This code is contributed by ukasp.`

## C#

 `//C# program for the above approach``using` `System;` `class` `GFG {` `    ``// Function to find  probability``    ``// such that x < y and X belongs to``    ``// arr1[] and Y belongs to arr2[]``    ``static` `double` `probability(``int``[] arr1, ``int``[] arr2)``    ``{``        ``// Stores the length of arr1``        ``int` `N = arr1.Length;` `        ``// Stores the length of arr2``        ``int` `M = arr2.Length;` `        ``// Stores the result``        ``double` `res = 0;` `        ``// Traverse the arr1[]``        ``for` `(``int` `i = 0; i < N; i++) {` `            ``// Stores the count of``            ``// elements in arr2 that``            ``// are greater than arr[i]``            ``int` `y = 0;` `            ``// Traverse the arr2[]``            ``for` `(``int` `j = 0; j < M; j++) {` `                ``// If arr2[j] is greater``                ``// than arr1[i]``                ``if` `(arr2[j] > arr1[i])``                    ``y++;``            ``}` `            ``// Increment res by y``            ``res += y;``        ``}` `        ``// Update the value of res``        ``res = (``double``)res / (``double``)(N * M);` `        ``// Return resultant probability``        ``return` `res;``    ``}` `    ``// Driver Code``    ``static` `void` `Main()``    ``{``        ``int``[] arr1 = { 5, 2, 6, 1 };``        ``int``[] arr2 = { 1, 6, 10, 1 };``        ``Console.WriteLine(probability(arr1, arr2));``    ``}``}`  `// This code is contributed by SoumikMondal.`

## Javascript

 ``

Output:

`0.4375`

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `int` `countGreater(``int``* arr, ``int` `k);` `// Function to find  probability``// such that x < y and X belongs``// to arr1[] & Y belongs to arr2[]``float` `probability(``int``* arr1,``                          ``int``* arr2)``{``    ``// Stores the length of arr1``    ``int` `N = 4;` `    ``// Stores the length of arr2``    ``int` `M = 4;` `    ``// Stores the result``    ``float` `res = 0;` `    ``// Sort the arr2[] in the``    ``// ascending order``    ``sort(arr2, arr2 + M);``    ` `    ``// Traverse the arr1[]``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Stores the count of``        ``// elements in arr2 that``        ``// are greater than arr[i]``        ``int` `y = countGreater(``            ``arr2, arr1[i]);``        ` `        ``// Increment res by y``        ``res += y;``    ``}` `    ``// Update the resultant``    ``// probability``    ``res = res / (N * M);` `    ``// Return the result``    ``return` `res;``}` `// Function to return the count``// of elements from the array``// which are greater than k``int` `countGreater(``int``* arr,``                        ``int` `k)``{``    ``int` `n = 4;``    ``int` `l = 0;``    ``int` `r = n - 1;` `    ``// Stores the index of the``    ``// leftmost element from the``    ``// array which is at least k``    ``int` `leftGreater = n;` `    ``// Finds number of elements``    ``// greater than k``    ``while` `(l <= r) {``        ``int` `m = l + (r - l) / 2;` `        ``// If mid element is at least``        ``// K, then update the value``        ``// of leftGreater and r``        ``if` `(arr[m] > k) {` `            ``// Update leftGreater``            ``leftGreater = m;` `            ``// Update r``            ``r = m - 1;``        ``}` `        ``// If mid element is``        ``// at most K, then``        ``// update the value of l``        ``else``            ``l = m + 1;``    ``}` `    ``// Return the count of``    ``// elements greater than k``    ``return` `(n - leftGreater);``}` `// Driver Code``int` `main()``{``    ``int` `arr1[] = { 5, 2, 6, 1 };``    ``int` `arr2[] = { 1, 6, 10, 1 };``    ``cout << probability(arr1, arr2);``    ``return` `0;``}` `// This code is contributed by Shubhamsingh10`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `    ``// Function to find  probability``    ``// such that x < y and X belongs``    ``// to arr1[] & Y belongs to arr2[]``    ``static` `double` `probability(``int``[] arr1,``                              ``int``[] arr2)``    ``{``        ``// Stores the length of arr1``        ``int` `N = arr1.length;` `        ``// Stores the length of arr2``        ``int` `M = arr2.length;` `        ``// Stores the result``        ``double` `res = ``0``;` `        ``// Sort the arr2[] in the``        ``// ascending order``        ``Arrays.sort(arr2);` `        ``// Traverse the arr1[]``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Stores the count of``            ``// elements in arr2 that``            ``// are greater than arr[i]``            ``int` `y = countGreater(``                ``arr2, arr1[i]);` `            ``// Increment res by y``            ``res += y;``        ``}` `        ``// Update the resultant``        ``// probability``        ``res = (``double``)res / (``double``)(N * M);` `        ``// Return the result``        ``return` `res;``    ``}` `    ``// Function to return the count``    ``// of elements from the array``    ``// which are greater than k``    ``static` `int` `countGreater(``int``[] arr,``                            ``int` `k)``    ``{``        ``int` `n = arr.length;``        ``int` `l = ``0``;``        ``int` `r = n - ``1``;` `        ``// Stores the index of the``        ``// leftmost element from the``        ``// array which is at least k``        ``int` `leftGreater = n;` `        ``// Finds number of elements``        ``// greater than k``        ``while` `(l <= r) {``            ``int` `m = l + (r - l) / ``2``;` `            ``// If mid element is at least``            ``// K, then update the value``            ``// of leftGreater and r``            ``if` `(arr[m] > k) {` `                ``// Update leftGreater``                ``leftGreater = m;` `                ``// Update r``                ``r = m - ``1``;``            ``}` `            ``// If mid element is``            ``// at most K, then``            ``// update the value of l``            ``else``                ``l = m + ``1``;``        ``}` `        ``// Return the count of``        ``// elements greater than k``        ``return` `(n - leftGreater);``    ``}` `    ``// Driver Code``    ``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

 `# Python3 program for the above approach` `# Function to find  probability``# such that x < y and X belongs``# to arr1[] & Y belongs to arr2[]``def` `probability(arr1, arr2):``  ` `    ``# Stores the length of arr1``    ``n ``=` `len``(arr1)``    ` `    ``# Stores the length of arr2``    ``m ``=` `len``(arr2)``    ` `    ``# Stores the result``    ``res ``=` `0``    ` `    ``# Sort the arr2[] in the``    ``# ascending order``    ``arr2.sort()``    ` `    ``# Traverse the arr1[]``    ``for` `i ``in` `range``(n):``        ` `        ``# Stores the count of``        ``# elements in arr2 that``        ``# are greater than arr[i]``        ``y ``=` `countGreater(arr2, arr1[i])``        ` `        ``# Increment res by y``        ``res ``+``=` `y``        ` `    ``# Update the resultant``    ``# probability``    ``res ``/``=` `(n ``*` `m)``    ` `    ``# Return the result``    ``return` `res` `# Function to return the count``# of elements from the array``# which are greater than k``def` `countGreater(arr, k):` `    ``n ``=` `len``(arr)``    ``l ``=` `0``    ``r ``=` `n ``-` `1``    ` `    ``# Stores the index of the``    ``# leftmost element from the``    ``# array which is at least k``    ``leftGreater ``=` `n``    ` `    ``# Finds number of elements``    ``# greater than k``    ``while` `l <``=` `r:``        ``m ``=` `(l ``+` `r) ``/``/` `2``        ` `        ``# If mid element is at least``        ``# K, then update the value``        ``# of leftGreater and r``        ``if` `(arr[m] > k):``            ` `            ``# Update leftGreater``            ``leftGreater ``=` `m``            ` `            ``# Update r``            ``r ``=` `m ``-` `1``            ` `        ``# If mid element is``        ``# at most K, then``        ``# update the value of l``        ``else``:``            ``l ``=` `m ``+` `1``            ` `    ``# Return the count of``    ``# elements greater than k``    ``return` `n ``-` `leftGreater` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr1 ``=` `[ ``5``, ``2``, ``6``, ``1` `]``    ``arr2 ``=` `[ ``1``, ``6``, ``10``, ``1` `]``    ` `    ``print``(probability(arr1, arr2))` `# This code is contributed by MuskanKalra1`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `   ``// Function to find  probability``    ``// such that x < y and X belongs``    ``// to arr1[] & Y belongs to arr2[]``    ``static` `double` `probability(``int``[] arr1,``                              ``int``[] arr2)``    ``{``      ` `        ``// Stores the length of arr1``        ``int` `N = arr1.Length;` `        ``// Stores the length of arr2``        ``int` `M = arr2.Length;` `        ``// Stores the result``        ``double` `res = 0;` `        ``// Sort the arr2[] in the``        ``// ascending order``        ``Array.Sort(arr2);` `        ``// Traverse the arr1[]``        ``for` `(``int` `i = 0; i < N; i++) {` `            ``// Stores the count of``            ``// elements in arr2 that``            ``// are greater than arr[i]``            ``int` `y = countGreater(``                ``arr2, arr1[i]);` `            ``// Increment res by y``            ``res += y;``        ``}` `        ``// Update the resultant``        ``// probability``        ``res = (``double``)res / (``double``)(N * M);` `        ``// Return the result``        ``return` `res;``    ``}` `    ``// Function to return the count``    ``// of elements from the array``    ``// which are greater than k``    ``static` `int` `countGreater(``int``[] arr,``                            ``int` `k)``    ``{``        ``int` `n = arr.Length;``        ``int` `l = 0;``        ``int` `r = n - 1;` `        ``// Stores the index of the``        ``// leftmost element from the``        ``// array which is at least k``        ``int` `leftGreater = n;` `        ``// Finds number of elements``        ``// greater than k``        ``while` `(l <= r) {``            ``int` `m = l + (r - l) / 2;` `            ``// If mid element is at least``            ``// K, then update the value``            ``// of leftGreater and r``            ``if` `(arr[m] > k) {` `                ``// Update leftGreater``                ``leftGreater = m;` `                ``// Update r``                ``r = m - 1;``            ``}` `            ``// If mid element is``            ``// at most K, then``            ``// update the value of l``            ``else``                ``l = m + 1;``        ``}` `        ``// Return the count of``        ``// elements greater than k``        ``return` `(n - leftGreater);``    ``}``  ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``       ``int``[] arr1 = { 5, 2, 6, 1 };``        ``int``[] arr2 = { 1, 6, 10, 1 };``        ``Console.Write(``            ``probability(arr1, arr2));``    ``}``}` `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output:

`0.4375`

Time Complexity: O(N * log M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up