499. Toss Strange Coins

You have n coins, each with a known probability of landing on heads.

Your task is to determine the probability that exactly target coins land on heads.

Example 1:

Input: prob = [0.4]  , target = 1  

Output: 0.40000  

Explanation:

We have 1 coin with a 40% probability (0.4) of landing on heads.

The only way to get exactly 1 heads is if this coin lands on heads.

Since there’s only one coin, the probability of this happening is 0.4.

Example 2:

Input: prob = [0.5, 0.5, 0.5, 0.5, 0.5] , target = 0

Output: 0.03125  

Explanation:

We have 5 coins, each with a 50% probability (0.5) of landing on heads.

We want 0 heads, meaning all 5 coins must land on tails.

The probability of a single coin landing on tails is 1 - 0.5 = 0.5.

Since there are 5 independent coins, the probability of all 5 landing on tails is: (0.5)5=0.03125(0.5)^5 = 0.03125(0.5)5=0.03125

Thus, the answer is 0.03125.

Now Your Turn!

Pick the correct output for the given input

Input: prob = [0.5, 0.5, 0.5] , target = 2

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= prob.length <= 1000
  • 0 <= prob[i] <= 1
  • 0 <= target <= prob.length
  • Answers will be accepted as correct if they are within 10-5 of the correct answer.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
double probabilityOfHeads(vector<double>& prob, int target) {
//Your Code Goes Here
}
};
Test Case

Input:

Prob
Target