Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

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

XPChallenge Points: 30
levelLevel: Hard

You have a total of 2n balls classified into k different colors. The array balls of length k specifies how many balls exist for each color. All balls are mixed randomly and from this shuffled arrangement, the first n balls are placed into Box 1 and the remaining n balls into Box 2.

The two boxes are considered distinct, meaning different ball placements into the boxes count as different outcomes.

Your task is to compute the probability that both Box 1 and Box 2 contain the same number of unique colors after the distribution. A result within 1e-5 of the actual probability will be accepted.

Example 1:

Input: balls = [2,2]

Output: 0.50000

Explanation:

Total balls: [A, A, B, B] Possible valid random assignments give half cases where both boxes have equal distinct colors. Probability = 1/2 = 0.5

Example 2:

Input: balls = [1,3]

Output: 1.00000

Explanation:

Box 1 must take 1 ball of the only 1-color group or from the 3-color group, but both boxes will always have exactly 1 distinct color. Probability = 1

Example 3:

Input: balls = [3,1,1]

Output: 0.42857

Explanation:

Shuffles create different patterns, some produce equal unique color counts and others don't. Final Probability ≈ 0.42857

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result