Probability of Keeping Majority Red Items
Math & Bit ManipulationAuto-graded · 25 test casesLast updated
Calculates the probability that at least two of three kept items are red when green items are returned upon selection.
A container contains three types of items: red red items, blue blue items, and green green items.
Items are drawn one at a time uniformly at random according to the following rules:
- If a green item is drawn, it is returned to the container.
- If a red or blue item is drawn, it is kept (removed from the container).
Drawing continues until exactly 3 items have been kept.
Return the probability that at least 2 of the 3 kept items are red.
Edge Case Handling
- If there are fewer than 2 red items (
red < 2), it is impossible to have at least 2 red items kept, so return0.0. - If the total number of non-green items is less than 3 (
red + blue < 3), it is impossible to collect 3 kept items, so return0.0.
Constraints
- $0 \le \text{red}, \text{blue}, \text{green} \le 10^4$
Examples
Example 1
Input: probability_at_least_two_red(5, 5, 5)
Output: 0.5
Example 2
Input: probability_at_least_two_red(10, 5, 5)
Output: 0.7582417582417582
Hints
Hint 1
Drawing a green item leaves the count of red and blue items unchanged. Does drawing green items affect the relative chances of drawing red vs blue?
Hint 2
Conditioned on drawing a non-green item, what is the probability of selecting a red item versus a blue item?
Hint 3
Since green draws do not alter the pool of red and blue items, the problem reduces to sampling 3 items without replacement from red and blue items only.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When a green item is selected, it is placed back into the container. The total counts of red and blue items in the container remain unchanged. Thus, drawing a green item merely delays the process without altering the relative probability distribution of the non-green items.
Conditioning on drawing a non-green (red or blue) item, each remaining red or blue item in the container is equally likely to be selected. Therefore, drawing until 3 non-green items are kept is mathematically equivalent to drawing 3 items without replacement directly from the pool of red red items and blue blue items.
We can compute the probability using combinatorics (hypergeometric distribution):
- Total outcomes: The total ways to select 3 non-green items from $R + B$ items is $\binom{R+B}{3}$.
- Favorable outcomes:
- Drawing exactly 2 red items and 1 blue item: $\binom{R}{2} \times \binom{B}{1} = \binom{R}{2} \times B$.
- Drawing exactly 3 red items and 0 blue items: $\binom{R}{3}$.
The required probability is: $$\frac{\binom{R}{2} \cdot B + \binom{R}{3}}{\binom{R+B}{3}}$$
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$ as binomial coefficients for small fixed sample sizes (3 items) take constant time.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space.
from math import comb
def probability_at_least_two_red(red: int, blue: int, green: int) -> float:
non_green = red + blue
if non_green < 3 or red < 2:
return 0.0
favorable = comb(red, 3) + comb(red, 2) * blue
return favorable / comb(non_green, 3)