Given four integers **p**, **q**, **r** and **s**. Two players are playing a game where both the players are hitting 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 as that of 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.307692308

Input: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 be **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 in any turn, Its 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) * t ^{2} + …**

This is an infinite GP series with common ratio less than 1 and it’s 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 winner ` `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 Code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` `import` `java.text.DecimalFormat; ` ` ` `class` `solution ` `{ ` ` ` `// Function to return the probability of the winner ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## C#

`// C# mplementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the probability of the winner ` `static` `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 Code ` `public` `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.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the probability ` `// of the winner ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.666666667

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Probability of A winning the match when individual probabilities of hitting the target given
- Find the player who wins the game by removing the last of given N cards
- Largest odd divisor Game to check which player wins
- Write a function that generates one of 3 numbers according to given probabilities
- Find the player who will win the Coin game
- Find the minimum possible health of the winning player
- Find amount to be added to achieve target ratio in a given mixture
- Find minimum moves to reach target on an infinite line
- Find the probability of reaching all points after N moves from point N
- Puzzle | Find The Probability of Distance in a Square
- Find probability of selecting element from kth column after N iterations
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Probability of getting a perfect square when a random number is chosen in a given range
- Minimum moves to reach target on a infinite line | Set 2
- Count of all possible ways to reach a target by a Knight
- Generate integer from 1 to 7 with equal probability
- Random number generator in arbitrary probability distribution fashion
- Probability of Knight to remain in the chessboard
- Probability of getting at least K heads in N tosses of Coins
- Aptitude | Probability | Question 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.