Group Size Threshold and Closest-Pair Gap on a Circle
Math & Bit ManipulationAuto-graded · 27 test casesLast updated
Compute a birthday-collision threshold exactly and by approximation, plus expected minimum gap and tail probability for random points on a circle, with a Poisson comparison.
Collisions and closest gaps
Implement a function that returns five numeric quantities from two related probability models.
Inputs
people(int): the number of random points used in the circle model (parts 3 to 5 below).dist(float): a distance threshold for the circle model.days(int, default365): the number of equally likely calendar days in the discrete birthday model.
Model A: discrete birthdays (uses only days)
Each person's birthday is uniform over days days, independently. Let the collision probability for a group of size k be the probability that at least two people share a birthday.
minimum_people: the smallest group sizekwhose exact collision probability is strictly greater than0.5. Build it by multiplying the no-collision factors one person at a time: personkcontributes the factor(days - k + 1) / days. If group size exceedsdays, a collision is certain (probability1), so the search can never go pastdays + 1. Return the value as a float.approximate_minimum_people: the closed-form estimatesqrt(2 * days * ln 2), as a float (not rounded).
Model B: random points on a circle (uses people and dist)
Place people points independently and uniformly on a circle of circumference 1. They split the circle into exactly people arcs, and the arc lengths are uniformly distributed over all non-negative tuples summing to 1. Let X be the smallest of these arcs (the distance between the closest pair).
expected_minimum_spacing:E[X] = 1 / people^2whenpeople > 0; return0.0whenpeople <= 0.probability_closest_distance_greater_than_d: the exact value ofP(X > dist), using these rules in order:- if
people <= 0ordist < 0:0.0 - else if
dist == 0:1.0 - else if
people * dist >= 1:0.0 - otherwise:
(1 - people * dist) ** (people - 1)
- if
poisson_approximation: the Poisson-style estimateexp(-people^2 * dist)whenpeople > 0anddist >= 0; otherwise0.0.
Output
Return a dictionary with exactly these keys, all with float values:
"minimum_people""approximate_minimum_people""expected_minimum_spacing""probability_closest_distance_greater_than_d""poisson_approximation"
Constraints
daysis a non-negative integer. Typical values are small to moderate, so anO(days)loop is fine.peopleis an integer and may be small or zero in edge cases. Handle it as specified above.distis a float and may be zero or negative in edge cases.- Model A does not depend on
peopleordist. Model B does not depend ondays. - Use ordinary double-precision floating point. Compare the collision probability to
0.5as1 - no_collision_product > 0.5.
Examples
Example 1
Input: birthday_threshold_and_gap_stats(300, 0.000001, 365)
Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.000011111111111111112, 'probability_closest_distance_greater_than_d': 0.9141931027462054, 'poisson_approximation': 0.9139311852712282}
Example 2
Input: birthday_threshold_and_gap_stats(300, 0.001, 365)
Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.000011111111111111112, 'probability_closest_distance_greater_than_d': 4.83408145724978e-47, 'poisson_approximation': 8.194012623990515e-40}
Example 3
Input: birthday_threshold_and_gap_stats(2, 0.1, 365)
Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.25, 'probability_closest_distance_greater_than_d': 0.8, 'poisson_approximation': 0.6703200460356393}
Hints
Hint 1
For the discrete model, keep a running product of the no-collision factors and stop at the first group size where one minus the product exceeds one half. Remember the pigeonhole cap at days + 1.
Hint 2
For the circle, the arcs are exchangeable. Requiring all of them to exceed dist amounts to shrinking the total length from 1 to 1 - people * dist and raising the ratio to the power people - 1.
Hint 3
The remaining outputs are direct formulas: sqrt(2 * days * ln 2), 1 / people^2, and exp(-people^2 * dist). Apply the guard rules for non-positive people and negative dist in the stated order.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The function combines two independent calculations.
Discrete birthdays. The probability that k people all have different birthdays is the product of (days - j) / days for j = 0 .. k-1. Person k contributes the factor (days - k + 1) / days. We multiply these factors one at a time and stop as soon as 1 - product > 0.5. That k is the exact threshold. If k would exceed days, a collision is guaranteed, so the product is set to 0 and the loop ends. This is the pigeonhole cap at days + 1.
For the approximation, use 1 - x ≈ e^(-x). The no-collision probability is then about exp(-k(k-1) / (2 * days)). Setting this equal to 1/2 gives k(k-1) ≈ 2 * days * ln 2, so k ≈ sqrt(2 * days * ln 2).
Points on a circle. people points cut the circle into people arcs. Their lengths are uniform on the simplex, which is a Dirichlet(1, ..., 1) distribution.
- To require every arc to exceed
dist, subtractdistfrom each arc. This leaves a simplex of total length1 - people * dist. Its volume scales by that factor raised to the powerpeople - 1, soP(X > dist) = (1 - people * dist)^(people - 1)forpeople * dist < 1, and0otherwise. - Integrating this tail over
distfrom0to1/peoplegivesE[X] = 1 / people^2. - The Poisson picture treats the gaps as roughly independent Exp(
people) variables, so their minimum is Exp(people^2). This givesexp(-people^2 * dist), which agrees with the exact tail to first order whenpeople * distis small.
The edge-case rules (non-positive people, negative dist, dist == 0) are applied in the order given in the statement.
Complexity
- Time:
O(days)for the threshold loop. Everything else isO(1). - Space:
O(1).
Reference solution
import math
from typing import Dict
def birthday_threshold_and_gap_stats(people: int, dist: float, days: int = 365) -> Dict[str, float]:
# Smallest group size whose exact collision probability exceeds 1/2.
no_collision = 1.0
threshold = 0
while 1.0 - no_collision <= 0.5:
threshold += 1
if threshold > days:
no_collision = 0.0
break
no_collision *= (days - threshold + 1) / days
approximate_threshold = math.sqrt(2.0 * days * math.log(2.0))
expected_min_spacing = 1.0 / (people * people) if people > 0 else 0.0
# All `people` circular arcs must exceed dist.
if people <= 0 or dist < 0:
probability = 0.0
elif dist == 0:
probability = 1.0
elif people * dist >= 1.0:
probability = 0.0
else:
probability = (1.0 - people * dist) ** (people - 1)
poisson = math.exp(-people * people * dist) if people > 0 and dist >= 0 else 0.0
return {
"minimum_people": float(threshold),
"approximate_minimum_people": approximate_threshold,
"expected_minimum_spacing": expected_min_spacing,
"probability_closest_distance_greater_than_d": probability,
"poisson_approximation": poisson,
}