Optimal Ball Distribution for Maximum Joint Red Probability
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Tests ability to formulate and optimize a probability function over discrete allocations with boundary analysis.
You have a collection of red balls and blue balls that must be placed into exactly two containers. Each container must hold at least one ball. You will then select one ball from the first container and one ball from the second container.
Your task is to determine the allocation that maximizes the probability that both selected balls are red, and return that maximum probability.
Write a function that takes the total number of red balls and the total number of blue balls, and returns the highest achievable probability (as a float) that both balls drawn are red.
Constraints:
0 ≤ red_balls ≤ 10^60 ≤ blue_balls ≤ 10^6- Both containers must be non-empty
- If it is impossible to draw two red balls (fewer than 2 red balls total), return
0.0
Examples
Example 1
Input: max_red_probability(100, 100)
Output: 0.49748743718592964
Example 2
Input: max_red_probability(10, 10)
Output: 0.47368421052631576
Example 3
Input: max_red_probability(2, 0)
Output: 1
Hints
Hint 1
The probability of both events is the product of two independent draws—one from each container.
Hint 2
One container can achieve probability 1.0 for drawing red if it contains only red balls. What's the minimum cost?
Hint 3
After isolating one red ball by itself, distribute all remaining balls to maximize the red ratio in the other container.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since we draw one ball from each container, the events are independent. The probability that both balls are red equals the product of the two individual probabilities:
$$P(\text{both red}) = P(\text{red from container 1}) \times P(\text{red from container 2})$$
Each probability is a fraction: the number of red balls in that container divided by the total balls in that container.
The key insight is that we can make one factor equal to 1 at minimal cost. Place exactly one red ball alone in the first container. This guarantees a red draw from that container with probability 1.
Now all remaining balls—both red and blue—go into the second container. With red_balls - 1 red balls and blue_balls blue balls in the second container, the probability of drawing red is:
$$\frac{\text{red_balls} - 1}{\text{red_balls} - 1 + \text{blue_balls}}$$
The overall probability becomes:
$$1 \times \frac{\text{red_balls} - 1}{\text{red_balls} - 1 + \text{blue_balls}}$$
This allocation is optimal. Any other distribution either:
- Wastes additional red balls in the first container (reducing the numerator in the second without improving the first factor beyond 1), or
- Splits blue balls between containers, which worsens both denominators compared to concentrating them in one container
Edge case: If there are fewer than 2 red balls, it's impossible to draw two red balls, so return 0.
Time Complexity: O(1) — direct arithmetic calculation
Space Complexity: O(1) — no additional storage needed
def max_red_probability(red_balls: int, blue_balls: int) -> float:
if red_balls < 2:
return 0.0
return (red_balls - 1) / (red_balls - 1 + blue_balls)