Ctrl K

Odds of Exactly That Many Heads

Medium
A magician owns a set of coins, and every one of them is unfair: coin i lands heads up with probability chances[i] (and tails with probability 1 - chances[i]). Tosses are independent. The magician tosses each coin exactly once. Find the probability that exactly want coins land heads up. An answer within 10⁻⁵ of the exact value is accepted.
Example 1
chances=[0.5, 0.5], want=1 → 0.5

Exactly one heads happens in two of the four equally likely outcomes, H-T and T-H, so 0.5.

Example 2
chances=[0.25, 1.0, 0.0], want=1 → 0.75

The second coin always lands heads and the third never does, so the first one must land tails: 1 − 0.25 = 0.75.

Example 3
chances=[0.5, 0.5, 0.5], want=3 → 0.125
Constraints
  • 1 ≤ len(chances) ≤ 1000
  • 0 ≤ chances[i] ≤ 1
  • 0 ≤ want ≤ len(chances)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Fair Split of Colored Beads HardNth Number Built From Given Primes MediumChain of Multiples MediumBest Rotation for a Weighted Sum MediumDuel for the End Cards Medium
loading Python…
Input
chances=[0.5, 0.5], want=1
Expected
0.5

Run checks the first 3 tests in your browser. Submit checks all 85 tests on the server, hidden ones included.