1432. Birthday Clash Odds

Easy1.5 points estimatedProbabilityMaths

A party planner seats people guests, and each guest's birthday is picked independently and uniformly from a calendar with days days. Return the probability that at least two guests share a birthday, as a double. If there are more guests than days, a clash is certain and the answer is 1.0. Otherwise compute the chance that all birthdays differ, which is the product of (days - i) / days for i from 0 to people - 1, and subtract it from one. An absolute error of 1e-6 is accepted. Run in O(people) time and O(1) extra space.

Example 1

Input:
people = 23, days = 365
Output:
0.5072972343239857
Explanation:

With 23 people and 365 days the chance that someone shares a birthday is about 0.5073.

Example 2

Input:
people = 2, days = 1
Output:
1
Explanation:

Two people but only one day always clash, so the probability is 1.0.

Constraints

1 ≤ people ≤ 105

1 ≤ days ≤ 105

Answers within an absolute error of 1e-6 are accepted.

How this problem is judged

Answers
Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
Tolerance
0.000001

Expected complexity

Time
O(people)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…