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 = [2,2,1,1]
Output: 0.60000
💡 Note: With 2 red, 2 blue, 1 green, 1 yellow balls (6 total, 3 per box). After calculating all valid distributions where both boxes have equal distinct colors, the probability is 0.60000

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

Visualization

Tap to expand
Probability: Same Distinct Colors in Two Boxes INPUT balls = [1, 1] 1 1 Color 0 Color 1 2n = 2 balls total A B k = 2 distinct colors n = 1 ball per box Box 1 (n balls) Box 2 (n balls) ALGORITHM STEPS 1 Enumerate Distributions List all ways to split balls 2 Count Valid Cases Both boxes: same distinct # 3 Calculate Probability valid / total permutations 4 Use Multinomial Coeff Handle identical balls Possible Distributions: Box1 Box2 Distinct [A] [B] 1 = 1 OK [B] [A] 1 = 1 OK All 2 distributions valid! FINAL RESULT Probability Calculation: Valid Cases: 2 Total Cases: 2 = 1.00000 100% Probability Both distributions give equal distinct # Key Insight: With balls=[1,1], each box gets exactly 1 ball. Since there are 2 colors and each box gets 1 ball, each box will always have exactly 1 distinct color. Both distributions [A][B] and [B][A] result in equal distinct counts (1=1), giving probability = 2/2 = 1.00000. Use DFS with multinomial coefficients for larger inputs. TutorialsPoint - Probability of Two Boxes Having Same Distinct Balls | Optimal Solution
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