Probability of A winning the match when individual probabilities of hitting the target given

Given four integers a, b, c and d. Player A & B try to score a penalty. Probability of A shooting the target is a / b while probability of B shooting the target is c / d. The player who scores the penalty first wins. The task is to find the probability of A winning the match.
Examples:Â
Â
Input: a = 1, b = 3, c = 1, d = 3Â
Output: 0.6
Input: a = 1, b = 2, c = 10, d = 11Â
Output: 0.52381Â
Â
Â
Approach: If we consider variables K = a / b as the probability of A shooting the target and R = (1 – (a / b)) * (1 – (c / d)) as the probability that A as well as B both missing the target.Â
Therefore, the solution forms a Geometric progression K * R0 + K * R1 + K * R2 + ….. whose sum is (K / 1 – R). After putting the values of K and R we get the formula as K * (1 / (1 – (1 – r) * (1 – k))).
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the probability of A winningdouble getProbability(int a, int b, int c, int d){Â
    // p and q store the values    // of fractions a / b and c / d    double p = (double)a / (double)b;    double q = (double)c / (double)d;Â
    // To store the winning probability of A    double ans = p * (1 / (1 - (1 - q) * (1 - p)));    return ans;}Â
// Driver codeint main(){Â Â Â Â int a = 1, b = 2, c = 10, d = 11;Â Â Â Â cout << getProbability(a, b, c, d);Â
    return 0;} |
Java
// Java implementation of the approachclass GFG {Â
// Function to return the probability// of A winningstatic double getProbability(int a, int b, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int d) {Â
    // p and q store the values    // of fractions a / b and c / d    double p = (double) a / (double) b;    double q = (double) c / (double) d;Â
    // To store the winning probability of A    double ans = p * (1 / (1 - (1 - q) *                                (1 - p)));    return ans;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int a = 1, b = 2, c = 10, d = 11;Â Â Â Â System.out.printf("%.5f", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â getProbability(a, b, c, d));}}Â
// This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach Â
# Function to return the probability# of A winning def getProbability(a, b, c, d) : Â
    # p and q store the values     # of fractions a / b and c / d     p = a / b;    q = c / d;         # To store the winning probability of A    ans = p * (1 / (1 - (1 - q) * (1 - p)));         return round(ans,5); Â
# Driver code if __name__ == "__main__" : Â
    a = 1; b = 2; c = 10; d = 11;     print(getProbability(a, b, c, d)); Â
# This code is contributed by Ryuga |
C#
// C# implementation of the approach using System;Â
class GFG{Â
// Function to return the probability // of A winning public static double getProbability(int a, int b, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int d){Â
    // p and q store the values     // of fractions a / b and c / d     double p = (double) a / (double) b;    double q = (double) c / (double) d;Â
    // To store the winning probability of A     double ans = p * (1 / (1 - (1 - q) *                                (1 - p)));    return ans;}Â
// Driver code public static void Main(string[] args){Â Â Â Â int a = 1, b = 2, c = 10, d = 11;Â Â Â Â Console.Write("{0:F5}", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â getProbability(a, b, c, d));}}Â
// This code is contributed by Shrikant13 |
PHP
<?php// PHP implementation of the approachÂ
// Function to return the probability // of A winningfunction getProbability($a, $b, $c, $d){Â
    // p and q store the values    // of fractions a / b and c / d    $p = $a / $b;    $q = $c / $d;Â
    // To store the winning probability of A    $ans = $p * (1 / (1 - (1 - $q) * (1 - $p)));    return round($ans,6);}Â
// Driver code$a = 1;$b = 2;$c = 10;$d = 11;echo getProbability($a, $b, $c, $d);Â
// This code is contributed by chandan_jnu?> |
Javascript
<script>Â
// JavaScript implementation of the approach   Â
// Function to return the probability// of A winning    function getProbability(a , b , c , d) {Â
        // p and q store the values        // of fractions a / b and c / d        var p = a / b;        var q = c / d;Â
        // To store the winning probability of A        var ans = p * (1 / (1 - (1 - q) * (1 - p)));        return ans;    }Â
    // Driver code             var a = 1, b = 2, c = 10, d = 11;        document.write( getProbability(a, b, c, d).toFixed(5));Â
// This code contributed by aashish1995Â
</script> |
0.52381
Â
 Time Complexity: O(1)
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


