##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1467.html

# 1467. Probability of a Two Boxes Having The Same Number of Distinct Balls (Hard)

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).

We want to calculate the probability that the two boxes have the same number of distinct balls.

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 equale 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


Example 4:

Input: balls = [3,2,1]
Output: 0.30000
Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box.
Probability = 18 / 60 = 0.3


Example 5:

Input: balls = [6,6,6,6,6,6]
Output: 0.90327


Constraints:

• 1 <= balls.length <= 8
• 1 <= balls[i] <= 6
• sum(balls) is even.
• Answers within 10^-5 of the actual value will be accepted as correct.

Related Topics:
Math, Backtracking

## Solution 1.

First, compute the total number of distinct shuffles.

total = factorial(sum( A[i] | 0 <= i < k )) / ( factorial(A[0]) * factorial(A[1]) * ... * factorial(A[k-1]) )


where factorial(x) is the number of permutations of x different items. For example, factorial(3) = 1 * 2 * 3 = 6.

I denote the right-hand side of the above equation as perm(A) where A is an array of numbers. We’ll need it later.

Then we need to compute the count of valid splits. Assume array a and b is a split of A, then:

• size(a) == size(b) == size(A) == k
• For each 0 <= i < k, a[i] + b[i] = A[i]

For example, if A = [1, 2, 2], then a = [1, 0, 1], b = [0, 2, 1] is a valid split of A.

A split is valid if:

• Each of them contains half of the balls: sum( a[i] | 0 <= i < k ) == sum( b[i] | 0 <= i < k ) == sum( A[i] | 0 <= i < k ) / 2
• They contain equal number of distinct colors: count(a) == count(b) where count(x) is the number of positive numbers in array x.

For example, if A = [1, 1, 2], then a = [1, 0, 1], b = [0, 1, 1] is a split of A.

So we can use DFS to get different valid splits of A. For each valid split a, b, the count of distinct permutation of the split is perm(a) * perm(b) .

The answer is total divided by the sum of perm(a) * perm(b) of all valid splits a, b.

// OJ: https://leetcode.com/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/
// Time: O((M+1)^K * K) where M is the maximum number of A[i]. It's O(7^8 * 8) given the constraints of this problem.
// Space: O(K)
class Solution {
double perm(vector<int> &A) {
double ans = 1;
for (int i = 0, j = 1; i < A.size(); ++i) {
int n = A[i];
for (int k = 1; k <= n; ++k, ++j) ans = ans * j / k;
}
return ans;
}
int sum = 0;
double dfs(vector<int> &A, vector<int>& a, vector<int> &b, int i, int sa, int sb) {
if (sa > sum / 2 || sb > sum / 2) return 0; // invalid split because either a or b takes up more than half of the balls.
if (i == A.size()) {
int ca = 0, cb = 0;
for (int i = 0; i < A.size(); ++i) ca += a[i] > 0;
for (int i = 0; i < A.size(); ++i) cb += b[i] > 0;
if (ca != cb) return 0; // invalid split because a and b don't have the same number of distinct colors.
return perm(a) * perm(b);
}
double ans = 0;
for (int j = 0; j <= A[i]; ++j) { // try different splits at the i-th element, i.e. a[i] + b[i] = A[i]
a[i] = j;
b[i] = A[i] - j;
ans += dfs(A, a, b, i + 1, sa + a[i], sb + b[i]);
}
a[i] = b[i] = 0;
return ans;
}
public:
double getProbability(vector<int>& A) {
sum = accumulate(begin(A), end(A), 0);
vector<int> a(A.size()), b(A.size());
return dfs(A, a, b, 0, 0, 0) / perm(A);
}
};

• class Solution {
double[] factorials = {1, 1, 2, 6, 24, 120, 720};
double sameDistinctCount = 0;

public double getProbability(int[] balls) {
double totalCount = calculatePermutations(balls);
backtrack(0, balls, new int[balls.length]);
return sameDistinctCount / totalCount;
}

public double calculatePermutations(int[] balls) {
int length = balls.length;
double[] total = new double[length];
total[0] = 1;
int sum = balls[0];
for (int i = 1; i < length; i++) {
total[i] = total[i - 1];
int max = sum + balls[i];
for (int j = sum + 1; j <= max; j++)
total[i] *= j;
total[i] /= factorials[balls[i]];
sum += balls[i];
}
}

public void backtrack(int start, int[] box1, int[] box2) {
int length = box1.length;
if (start == length) {
int count1 = 0, count2 = 0, sum1 = 0, sum2 = 0;
for (int i = 0; i < length; i++) {
sum1 += box1[i];
sum2 += box2[i];
if (box1[i] > 0)
count1++;
if (box2[i] > 0)
count2++;
}
if (count1 == count2 && sum1 == sum2)
sameDistinctCount += calculatePermutations(box1) * calculatePermutations(box2);
} else {
int max = box1[start];
for (int i = 0; i <= max; i++) {
box1[start] -= i;
box2[start] += i;
backtrack(start + 1, box1, box2);
box1[start] += i;
box2[start] -= i;
}
}
}
}

• class Solution:
def getProbability(self, balls: List[int]) -> float:
@cache
def dfs(i: int, j: int, diff: int) -> float:
if i >= k:
return 1 if j == 0 and diff == 0 else 0
if j < 0:
return 0
ans = 0
for x in range(balls[i] + 1):
y = 1 if x == balls[i] else (-1 if x == 0 else 0)
ans += dfs(i + 1, j - x, diff + y) * comb(balls[i], x)
return ans

n = sum(balls) >> 1
k = len(balls)
return dfs(0, n, 0) / comb(n << 1, n)


• func getProbability(balls []int) float64 {
n, mx := 0, 0
for _, x := range balls {
n += x
mx = max(mx, x)
}
n >>= 1
m := max(mx, n<<1)
c := make([][]int, m+1)
for i := range c {
c[i] = make([]int, m+1)
}
for i := 0; i <= m; i++ {
c[i][0] = 1
for j := 1; j <= i; j++ {
c[i][j] = c[i-1][j-1] + c[i-1][j]
}
}
k := len(balls)
f := make([][][]int, k)
for i := range f {
f[i] = make([][]int, n+1)
for j := range f[i] {
f[i][j] = make([]int, k<<1|1)
for h := range f[i][j] {
f[i][j][h] = -1
}
}
}
var dfs func(int, int, int) int
dfs = func(i, j, diff int) int {
if i >= k {
if j == 0 && diff == k {
return 1
}
return 0
}
if j < 0 {
return 0
}
if f[i][j][diff] != -1 {
return f[i][j][diff]
}
ans := 0
for x := 0; x <= balls[i]; x++ {
y := 1
if x != balls[i] {
if x == 0 {
y = -1
} else {
y = 0
}
}
ans += dfs(i+1, j-x, diff+y) * c[balls[i]][x]
}
f[i][j][diff] = ans
return ans
}
return float64(dfs(0, n, k)) / float64(c[n<<1][n])
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

• function getProbability(balls: number[]): number {
const n = balls.reduce((a, b) => a + b, 0) >> 1;
const mx = Math.max(...balls);
const m = Math.max(mx, n << 1);
const c: number[][] = Array(m + 1)
.fill(0)
.map(() => Array(m + 1).fill(0));
for (let i = 0; i <= m; ++i) {
c[i][0] = 1;
for (let j = 1; j <= i; ++j) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
const k = balls.length;
const f: number[][][] = Array(k)
.fill(0)
.map(() =>
Array(n + 1)
.fill(0)
.map(() => Array((k << 1) | 1).fill(-1)),
);
const dfs = (i: number, j: number, diff: number): number => {
if (i >= k) {
return j === 0 && diff === k ? 1 : 0;
}
if (j < 0) {
return 0;
}
if (f[i][j][diff] !== -1) {
return f[i][j][diff];
}
let ans = 0;
for (let x = 0; x <= balls[i]; ++x) {
const y = x === balls[i] ? 1 : x === 0 ? -1 : 0;
ans += dfs(i + 1, j - x, diff + y) * c[balls[i]][x];
}
return (f[i][j][diff] = ans);
};
return dfs(0, n, k) / c[n << 1][n];
}

`