Watch 6 video solutions for Probability of a Two Boxes Having The Same Number of Distinct Balls, a hard level problem involving Array, Math, Dynamic Programming. This walkthrough by Roshan Srivastava has 1,825 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.
All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully).
Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully).
Return the probability that the two boxes have the same number of distinct balls. Answers within 10-5 of the actual value will be accepted as correct.
Example 1:
Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1
Example 2:
Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equal probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that, we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667
Example 3:
Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6
Constraints:
1 <= balls.length <= 81 <= balls[i] <= 6sum(balls) is even.Problem Overview: You are given an array where balls[i] represents the number of balls of color i. All balls are randomly split into two boxes with the same total number of balls. The goal is to compute the probability that both boxes end up containing the same number of distinct colors.
Approach 1: Backtracking with Combinatorics (Exponential time, O(k) space)
Enumerate every valid way to distribute balls of each color between the two boxes while ensuring both boxes end with exactly total/2 balls. For each color i, iterate from 0..balls[i] to decide how many go to box A and place the rest in box B. Track three values during recursion: balls count in each box, number of distinct colors in each box, and the number of permutations for that configuration. Use factorial-based combinations (C(n, k)) to count how many permutations each distribution contributes. If both boxes have the same number of distinct colors, add its permutations to the valid count. The probability equals valid_permutations / total_permutations. This approach relies on combinatorics to count arrangements and backtracking to explore distributions. Time complexity is roughly O(∏(balls[i] + 1)) which is manageable because the number of colors is small (≤ 8). Space complexity is O(k) for recursion depth.
Approach 2: Dynamic Programming with Combinatorics (O(k * n^3) time, O(n^3) space)
Instead of enumerating every distribution explicitly, model the process as a DP over colors. Let the state track: how many balls are placed in box A, how many distinct colors appear in box A, and how many appear in box B after processing the first i colors. For each color, try all splits of its balls between the two boxes and update the DP using the number of ways given by C(balls[i], split). Precompute factorials to evaluate combinations quickly. At the end, sum all states where both boxes contain total/2 balls and the distinct color counts match. This reduces repeated recomputation from brute enumeration and uses dynamic programming to aggregate counts efficiently. Time complexity is approximately O(k * n^3), where k is the number of colors and n is half the total balls. Space complexity is O(n^3) for the DP table.
Recommended for interviews: The backtracking + combinatorics approach is the most common explanation because it directly models the distribution process and demonstrates strong understanding of counting and probability. Interviewers expect you to combine recursion with factorial-based combination counting. The DP version is useful when discussing optimization and overlapping subproblems, but most candidates solve the problem using backtracking with careful combinatorics.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Combinatorics | O(∏(balls[i]+1)) | O(k) | Best for understanding the distribution logic and typical interview explanations. |
| Dynamic Programming with Combinatorics | O(k * n^3) | O(n^3) | Useful when optimizing repeated calculations or discussing DP state transitions. |