Given two coins which have probability of getting heads p% and q% respectively, the task is to determine the probability of getting two consecutive heads after choosing random coins among the given coins.
Examples:
Input: p = 33, q = 66
Output: 0.550000000000000
Input: p = 33, q = 66
Output: 0.550000000000000
Approach:
Since both the coins are not identical so Bayes’s theorem will be used to get the desired probability.
As the coins are to be chosen randomly then any of them can be chosen so p and q will be included in the calculation. After applying Bayes’s theorem, the required answer will be (p * p + q * q) / (p + q) because if the first coin is chosen then the probability of getting both heads back to back is p * p and same for the second coin.
It’s an application of Bayes’ theorem.
P(B | A) = P(A |^| B) / P(A) = (1/2 * p * p + 1/2 * q * q) / (1/2 * p + 1/2 * q) = (p * p + q * q) / (p + q) where;
P(B) = probability to get heads on the second throw,
P(A) = probability to get heads on the first throw and
P(A |^| B) = probability to get heads on both the throws.
So, P(B | A) is the probability to get heads on the second throw if we are given that we got heads
on the first one.
Here A, B denotes 1st and 2nd coin.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double getProbability( double p, double q)
{
p /= 100;
q /= 100;
double probability = (p * p + q * q) / (p + q);
return probability;
}
int main()
{
double p, q;
p = 80;
q = 40;
cout << fixed
<< setprecision(15)
<< getProbability(p, q)
<< endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static double getProbability( double p, double q)
{
p /= 100 ;
q /= 100 ;
double probability = (p * p + q * q) / (p + q);
return probability;
}
public static void main (String[] args) {
double p, q;
p = 80 ;
q = 40 ;
System.out.println( getProbability(p, q));
}
}
|
Python 3
def getProbability(p, q):
p / = 100
q / = 100
probability = (p * p + q * q) / (p + q)
return probability
if __name__ = = "__main__" :
p = 80
q = 40
print (getProbability(p, q))
|
C#
using System;
class GFG {
static double getProbability( double p, double q)
{
p /= 100;
q /= 100;
double probability = (p * p + q * q) / (p + q);
return probability;
}
public static void Main () {
double p, q;
p = 80;
q = 40;
Console.WriteLine( getProbability(p, q));
}
}
|
PHP
<?php
function getProbability( $p , $q )
{
$p /= 100;
$q /= 100;
$probability = ( $p * $p + $q * $q ) /
( $p + $q );
return $probability ;
}
$p = 80;
$q = 40;
echo getProbability( $p , $q );
?>
|
Javascript
<script>
function getProbability(p, q)
{
p /= 100;
q /= 100;
let probability = (p * p + q * q) / (p + q);
return probability;
}
let p = 80.0;
let q = 40.0;
document.write(getProbability(p, q).toPrecision(15));
</script>
|
Output:
0.666666666666667
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 Jun, 2022
Like Article
Save Article