Tuesday, June 28, 2022
HomeSoftware DevelopmentLikelihood of hitting the goal Nth time at Mth throw

Likelihood of hitting the goal Nth time at Mth throw

Improve Article

Save Article

Like Article

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.


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 thrwos 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.



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


def fact(f):

    fact = 1

    for i in range(1, f + 1):

        fact = i * fact

    return fact



if __name__ == '__main__':

    p = 0.3

    M = 2

    N = 1

    print(probab(p, M, N))

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



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments