给定两个数组arr1[]和arr2[] 分别由N和M 个整数组成,任务是找到分别从arr1[]和arr2[]中随机选择这两个数字的概率,使得第一个选择的元素严格小于比第二个选定的元素。
例子:
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
朴素的方法:可以根据以下观察解决给定的问题:
- 这个想法是使用条件概率的概念。从数组arr1[]中选择一个元素的概率是1/N。
- 现在假设X是arr2[]中元素的计数大于arr1[]的选定元素,那么从arr2[]中选择一个这样的元素的概率是X/M 。
- 因此,选择两个元素使得第一个元素小于第二个所选元素的概率是arr1[] 中每个元素的(1/N)*(X/M) 之和。
请按照以下步骤解决问题:
- 初始化一个变量,比如res为0来存储结果概率。
- 遍历给定的数组arr1[]并执行以下步骤:
- 通过遍历数组arr2[]找到arr2[] 中大于arr1[i]的元素计数,然后通过它增加res 。
- 将res的值更新为res = res/N*M 。
- 完成上述步骤后,打印res的值作为结果概率。
下面是上述方法的实现:
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 arr1,vector 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 arr1 = { 5, 2, 6, 1 };
vector 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
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
0.4375
时间复杂度: O(N * M)
辅助空间: O(1)
高效方法:上述方法可以通过使用二分搜索进行优化。请按照以下步骤解决问题:
- 初始化一个变量,比如res为0来存储结果概率。
- 按升序对数组进行排序。
- 遍历给定的数组arr1[]并执行以下步骤:
- 通过使用二分搜索找到arr2[] 中大于arr1[i]的元素计数,然后通过它增加res 。
- 将res的值更新为res = res/N*M。
- 完成上述步骤后,打印res作为结果概率。
下面是上述方法的实现:
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));
}
}
蟒蛇3
# 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
0.4375
时间复杂度: O(N * log M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。