1467. Probability of a Two Boxes Having The Same Number of Distinct Balls LeetCode Solution
In this guide, you will get 1467. Probability of a Two Boxes Having The Same Number of Distinct Balls LeetCode Solution with the best time and space complexity. The solution to Probability of a Two Boxes Having The Same Number of Distinct Balls problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Probability of a Two Boxes Having The Same Number of Distinct Balls solution in C++
- Probability of a Two Boxes Having The Same Number of Distinct Balls solution in Java
- Probability of a Two Boxes Having The Same Number of Distinct Balls solution in Python
- Additional Resources

Problem Statement of Probability of a Two Boxes Having The Same Number of Distinct Balls
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 <= 8
1 <= balls[i] <= 6
sum(balls) is even.
Complexity Analysis
- Time Complexity: O((n / k)^k)
- Space Complexity: O(k)
1467. Probability of a Two Boxes Having The Same Number of Distinct Balls LeetCode Solution in C++
enum class BoxCase { kEqualBalls, kEqualDistantBalls };
class Solution {
public:
double getProbability(vector<int>& balls) {
const int n = accumulate(balls.begin(), balls.end(), 0) / 2;
return cases(balls, 0, 0, 0, 0, 0, n, BoxCase::kEqualDistantBalls) /
cases(balls, 0, 0, 0, 0, 0, n, BoxCase::kEqualBalls);
}
private:
const vector<int> fact{1, 1, 2, 6, 24, 120, 720};
// Assume we have two boxes A and B.
double cases(const vector<int>& balls, int i, int ballsCountA,
int ballsCountB, int colorsCountA, int colorsCountB, int n,
BoxCase boxCase) {
if (ballsCountA > n || ballsCountB > n)
return 0;
if (i == balls.size())
return boxCase == BoxCase::kEqualBalls ? 1 : colorsCountA == colorsCountB;
double ans = 0;
// balls taken from A for `balls[i]`
for (int ballsTakenA = 0; ballsTakenA <= balls[i]; ++ballsTakenA) {
const int ballsTakenB = balls[i] - ballsTakenA;
const int newcolorsCountA = colorsCountA + (ballsTakenA > 0);
const int newcolorsCountB = colorsCountB + (ballsTakenB > 0);
ans += cases(balls, i + 1, ballsCountA + ballsTakenA,
ballsCountB + ballsTakenB, newcolorsCountA, newcolorsCountB,
n, boxCase) /
(fact[ballsTakenA] * fact[ballsTakenB]);
}
return ans;
}
};
/* code provided by PROGIEZ */
1467. Probability of a Two Boxes Having The Same Number of Distinct Balls LeetCode Solution in Java
enum BoxCase { kEqualBalls, kEqualDistantBalls }
class Solution {
public double getProbability(int[] balls) {
final int n = Arrays.stream(balls).sum() / 2;
return cases(balls, 0, 0, 0, 0, 0, n, BoxCase.kEqualDistantBalls) /
cases(balls, 0, 0, 0, 0, 0, n, BoxCase.kEqualBalls);
}
private int[] fact = {1, 1, 2, 6, 24, 120, 720};
// Assume we have two boxes A and B.
double cases(int[] balls, int i, int ballsCountA, int ballsCountB, int colorsCountA,
int colorsCountB, int n, BoxCase boxCase) {
if (ballsCountA > n || ballsCountB > n)
return 0;
if (i == balls.length)
return boxCase == BoxCase.kEqualBalls ? 1 : (colorsCountA == colorsCountB ? 1 : 0);
double ans = 0;
// balls taken from A for `balls[i]`
for (int ballsTakenA = 0; ballsTakenA <= balls[i]; ++ballsTakenA) {
final int ballsTakenB = balls[i] - ballsTakenA;
final int newcolorsCountA = colorsCountA + (ballsTakenA > 0 ? 1 : 0);
final int newcolorsCountB = colorsCountB + (ballsTakenB > 0 ? 1 : 0);
ans += cases(balls, i + 1, ballsCountA + ballsTakenA, ballsCountB + ballsTakenB,
newcolorsCountA, newcolorsCountB, n, boxCase) /
(fact[ballsTakenA] * fact[ballsTakenB]);
}
return ans;
}
}
// code provided by PROGIEZ
1467. Probability of a Two Boxes Having The Same Number of Distinct Balls LeetCode Solution in Python
from enum import Enum
class BoxCase(Enum):
kEqualDistantBalls = 0
kEqualBalls = 1
class Solution:
def getProbability(self, balls: list[int]) -> float:
n = sum(balls) // 2
fact = [1, 1, 2, 6, 24, 120, 720]
def cases(
i: int,
ballsCountA: int,
ballsCountB: int,
colorsCountA: int,
colorsCountB,
boxCase: BoxCase) -> float:
if ballsCountA > n or ballsCountB > n:
return 0
if i == len(balls):
return (1 if boxCase == BoxCase.kEqualBalls
else colorsCountA == colorsCountB)
ans = 0.0
# balls taken from A for `balls[i]`
for ballsTakenA in range(balls[i] + 1):
ballsTakenB = balls[i] - ballsTakenA
newcolorsCountA = colorsCountA + (ballsTakenA > 0)
newcolorsCountB = colorsCountB + (ballsTakenB > 0)
ans += (cases(i + 1,
ballsCountA + ballsTakenA,
ballsCountB + ballsTakenB,
newcolorsCountA, newcolorsCountB, boxCase) /
(fact[ballsTakenA] * fact[ballsTakenB]))
return ans
return (cases(0, 0, 0, 0, 0, BoxCase.kEqualDistantBalls) /
cases(0, 0, 0, 0, 0, BoxCase.kEqualBalls))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.