Probability of a Two Boxes Having The Same Number of Distinct Balls - Problem

Imagine you're a statistician at a game show where contestants must predict probability outcomes! You have 2n balls of k distinct colors, and you need to calculate the probability that two boxes end up with the same number of distinct colors after a random distribution.

Given an integer array balls of size k where balls[i] represents the number of balls of color i, all balls are shuffled uniformly at random. Then, the first n balls go to Box 1 and the remaining n balls go to Box 2.

Important: The two boxes are considered different. For example, with balls of colors 'a' and 'b', the distribution [a] (b) is different from [b] (a).

Goal: Return the probability that both boxes have the same number of distinct ball colors. Your answer should be accurate within 10-5 of the actual value.

Input & Output

example_1.py โ€” Simple Case
$ Input: balls = [1,1]
โ€บ Output: 1.00000
๐Ÿ’ก Note: With 1 red ball and 1 blue ball, we have only one way to distribute them: one ball per box. Both distributions ([red][blue] and [blue][red]) result in each box having exactly 1 distinct color, so probability = 1.0
example_2.py โ€” More Complex
$ Input: balls = [2,1,1]
โ€บ Output: 0.66667
๐Ÿ’ก Note: With 2 red, 1 blue, 1 green balls (4 total, 2 per box). Valid equal distributions: [RB][RG], [RG][RB], [BR][GR], [GR][BR] - each box has 2 colors. Total favorable outcomes = 8, total outcomes = 12, so probability = 8/12 = 2/3
example_3.py โ€” Edge Case
$ Input: balls = [1,2,1,1]
โ€บ Output: 0.60000
๐Ÿ’ก Note: With 5 balls total, we need 2.5 per box which is impossible. Wait - we have 1+2+1+1=5 balls total, but we need equal distribution. Actually, this should work with 2-3 distribution per the problem constraints.

Visualization

Tap to expand
Ball Distribution ProbabilityInitial: [2 Red, 1 Blue, 1 Green]Box 1Box 22 distinct colors2 distinct colorsโœ“ Valid DistributionBoth boxes have exactly 2 distinct colorsProbability = (Ways to achieve this) / (Total ways)P = 2/3 โ‰ˆ 0.66667
Understanding the Visualization
1
Initial Setup
Start with all balls of different colors, need to split into two equal-sized boxes
2
Try Distribution
For each color, decide how many balls go to each box
3
Check Validity
Ensure both boxes have equal number of balls and same distinct colors
4
Calculate Probability
Use multinomial coefficients to find probability of this specific distribution
Key Takeaway
๐ŸŽฏ Key Insight: Instead of enumerating all possible ball arrangements, we work with color distributions and use multinomial coefficients to efficiently calculate the probability of each valid scenario where both boxes have equal distinct colors.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(4^k)

For each color, we try 0 to balls[i] in first box, but pruning reduces actual complexity significantly

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Recursive call stack depth equals number of colors

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค balls.length โ‰ค 8
  • 1 โ‰ค balls[i] โ‰ค 6
  • 2 * n == sum(balls) where n is the number of balls per box
  • Answers within 10-5 of actual value are accepted
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen