Given four integers p, q, r, and s. Two players are playing a game where both the players hit a target and the first player who hits the target wins the game. The probability of the first player hitting the target is p / q and that of the second player hitting the target is r / s. The task is to find the probability of the first player winning the game.
Examples:Â
Input: p = 1, q = 4, r = 3, s = 4Â
Output: 0.307692308Input: p = 1, q = 2, r = 1, s = 2Â
Output: 0.666666667Â
Approach: The probability of the first player hitting the target is p / q and missing the target is 1 – p / q.Â
The probability of the second player hitting the target is r / s and missing the target is 1 – r / s.Â
Let the first player be x and the second player is y.Â
So the total probability will be x won + (x lost * y lost * x won) + (x lost * y lost * x lost * y lost * x won) + … so on.Â
Because x can win at any turn, it’s an infinite sequence.Â
Let t = (1 – p / q) * (1 – r / s). Here t < 1 as p / q and r / s are always <1.Â
So the series will become, p / q + (p / q) * t + (p / q) * t2 + …Â
This is an infinite GP series with a common ratio of less than 1 and its sum will be (p / q) / (1 – t).
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 the winnerdouble find_probability(double p, double q,                        double r, double s){Â
    double t = (1 - p / q) * (1 - r / s);Â
    double ans = (p / q) / (1 - t);Â
    return ans;}Â
// Driver Codeint main(){Â Â Â Â double p = 1, q = 2, r = 1, s = 2;Â
    // Will print 9 digits after the decimal point    cout << fixed << setprecision(9)         << find_probability(p, q, r, s);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;import java.text.DecimalFormat;Â
class solution{Â
// Function to return the probability of the winnerstatic double find_probability(double p, double q,                        double r, double s){Â
    double t = (1 - p / q) * (1 - r / s);Â
    double ans = (p / q) / (1 - t);Â
    return ans;}Â
// Driver Codepublic static void main(String args[]){Â Â Â Â double p = 1, q = 2, r = 1, s = 2;Â
    // Will print 9 digits after the decimal point    DecimalFormat dec = new DecimalFormat("#0.000000000");    System.out.println(dec.format(find_probability(p, q, r, s)));}}// This code is contributed by// Surendra_Gangwar |
Python3
# Python3 implementation of the approach Â
# Function to return the probability# of the winner def find_probability(p, q, r, s) :Â Â Â Â Â Â Â Â Â t = (1 - p / q) * (1 - r / s)Â
    ans = (p / q) / (1 - t); Â
    return round(ans, 9)Â
# Driver Code if __name__ == "__main__" : Â
    p, q, r, s = 1, 2, 1, 2Â
    # Will print 9 digits after     # the decimal point     print(find_probability(p, q, r, s)) Â
# This code is contributed by Ryuga |
C#
// C# implementation of the approachusing System;Â
class GFG{Â
// Function to return the probability of the winnerstatic double find_probability(double p, double q,                        double r, double s){Â
    double t = (1 - p / q) * (1 - r / s);Â
    double ans = (p / q) / (1 - t);Â
    return ans;}Â
// Driver Codepublic static void Main(){Â Â Â Â double p = 1, q = 2, r = 1, s = 2;Â Â Â Â Console.WriteLine(find_probability(p, q, r, s));}}Â
// This code is contributed by// anuj_67.. |
PHP
<?php// PHP implementation of the approachÂ
// Function to return the probability// of the winnerfunction find_probability($p, $q, $r, $s){Â Â Â Â $t = (1 - $p / $q) * (1 - $r / $s);Â
    $ans = ($p / $q) / (1 - $t);Â
    return $ans;}Â
// Driver Code$p = 1; $q = 2;$r = 1; $s = 2;Â
// Will print 9 digits after // the decimal point$res = find_probability($p, $q, $r, $s);$update = number_format($res, 7);echo $update;Â
// This code is contributed by Rajput-Ji?> |
Javascript
<script>Â
Â
// Javascript implementation of the approachÂ
// Function to return the probability of the winnerfunction find_probability(p, q, r, s){Â
    var t = (1 - p / q) * (1 - r / s);Â
    var ans = (p / q) / (1 - t);Â
    return ans;}Â
// Driver Codevar p = 1, q = 2, r = 1, s = 2;// Will print 9 digits after the decimal pointdocument.write( find_probability(p, q, r, s).toFixed(9));Â
Â
</script> |
0.666666667
Â
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 neveropen!
