# Probability of hitting the target Nth time at Mth throw

• Difficulty Level : Hard
• Last Updated : 13 Jun, 2022

Given integers N, M and p, the task is to find the probability of hitting a target for the Nth time at the Mth throw where p is the probability of hitting the target.

Examples:

Input: N = 1, M = 2, p = 0.3
Output: 0.21
Explanation: The target can be hit for the first time in the second throw only when the first throw is a miss.
So, the probability is multiplication of probability of hitting the target in second throw and probability of missing the target in the first throw = 0.3 * 0.7 = 0.21
because probability of missing  = 1 – probability of hitting.

Input: N = 8, M = 17, p = 0.4,
Output: 0.07555569565040642

Approach: The idea to solve the problem is based on the binomial distribution of probability

For getting Nth hit in the Mth throw there must be N-1 hits in the M-1 throws earlier and Nth throw must be a hit.
Say p is the probability of hitting and q is the probability of missing. q = 1 – p.
So the probability of getting N-1 hits in M-1 throws is:
X = M-1CN-1 (pN-1qM-1-(N-1)) = M-1CN-1 (pN-1qM-N)
Therefore, the probability of hitting for Nth time is p*X = M-1CN-1 (pNqM-N)

Follow the below steps to implement the above idea:

• Firstly, get the probability of not hitting a target.
• Get the value of X as shown above.
• Multiply the value of ‘p’ with it to get the actual answer.

Below is the implementation of the above approach.

## C++

 `// Cpp code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function to find the factorial of a number` `int` `fact(``int` `f)` `{` `    ``int` `fact = 1;` `    ``for` `(``int` `i = 1; i <= f; i++)` `        ``fact = i * fact;` `    ``return` `fact;` `}`   `// Function to find the probability` `// of Nth hit at Mth throw` `double` `probab(``double` `p, ``int` `m, ``int` `n)` `{` `    ``double` `q = 1 - p;` `    ``double` `res = fact(m - 1) / fact(n - 1) / fact(m - n)` `                 ``* ``pow``(p, (n - 1)) * ``pow``(q, (m - n)) * p;` `    ``return` `res;` `}`   `// Driver code` `int` `main()` `{` `    ``double` `p = 0.3;` `    ``int` `M = 2;` `    ``int` `N = 1;` `  `  `    ``// Function call` `    ``cout << probab(p, M, N);` `    ``return` `0;` `}`   `// This code is contributed by ANKITKUMAR34.`

## Java

 `// Java code for above approach`   `import` `java.util.*;`   `class` `GFG {` `    ``// Function to find the factorial of a number` `    ``public` `static` `int` `fact(``int` `f)` `    ``{` `        ``int` `fact = ``1``;` `        ``for` `(``int` `i = ``1``; i <= f; i++)` `            ``fact = i * fact;` `        ``return` `fact;` `    ``}`   `    ``// Function to find the probability` `    ``// of Nth hit at Mth throw` `    ``public` `static` `double` `probab(``double` `p, ``int` `m, ``int` `n)` `    ``{` `        ``double` `q = ``1` `- p;` `        ``double` `res = fact(m - ``1``) / fact(n - ``1``) / fact(m - n)` `                     ``* Math.pow(p, (n - ``1``))` `                     ``* Math.pow(q, (m - n)) * p;` `        ``return` `res;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``double` `p = ``0.3``;` `        ``int` `M = ``2``;` `        ``int` `N = ``1``;`   `        ``// Function call` `        ``System.out.println(probab(p, M, N));` `    ``}` `}`   `// This code is contributed by Pushpesh Raj.`

## Python3

 `# Python code to implement the approach`   `# Function to find the probability` `# of Nth hit at Mth throw` `def` `probab(p, m, n):` `    ``q ``=` `1``-``p` `    ``res ``=` `fact(m``-``1``)``/``fact(n``-``1``)``/``fact(m``-``n)``*``p``*``*``(n``-``1``)``*``q``*``*``(m``-``n)``*``p` `    ``return` `res`   `# Function to find the factorial of a number` `def` `fact(f):` `    ``fact ``=` `1` `    ``for` `i ``in` `range``(``1``, f ``+` `1``):` `        ``fact ``=` `i ``*` `fact` `    ``return` `fact`     `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``p ``=` `0.3` `    ``M ``=` `2` `    ``N ``=` `1` `    `  `    ``# Function call` `    ``print``(probab(p, M, N))`

## C#

 `// C# code for above approach` `using` `System;`   `public` `class` `GFG ` `{`   `  ``// Function to find the factorial of a number` `  ``public` `static` `int` `fact(``int` `f)` `  ``{` `    ``int` `fact = 1;` `    ``for` `(``int` `i = 1; i <= f; i++)` `      ``fact = i * fact;` `    ``return` `fact;` `  ``}`   `  ``// Function to find the probability` `  ``// of Nth hit at Mth throw` `  ``public` `static` `double` `probab(``double` `p, ``int` `m, ``int` `n)` `  ``{` `    ``double` `q = 1 - p;` `    ``double` `res = fact(m - 1) / fact(n - 1) / fact(m - n)` `      ``* Math.Pow(p, (n - 1))` `      ``* Math.Pow(q, (m - n)) * p;` `    ``return` `res;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main(``string``[] args)` `  ``{` `    ``double` `p = 0.3;` `    ``int` `M = 2;` `    ``int` `N = 1;`   `    ``// Function call` `    ``Console.WriteLine(probab(p, M, N));` `  ``}` `}`   `// This code is contributed by AnkThon`

## Javascript

 ``

Output

`0.21`

Time Complexity: O(M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles