Brainstorming Quiz, Day - 6 Solution
Learn how to calculate the optimal arrival time to maximize your chances of winning based on the birthday paradox, explained step-by-step with a simple C++ program.
Sandip Sapkota
Sun, Dec 8, 2024
2 min read
Answer:13:01 (1:01) PM
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int findTime(double p) {
return ceil(sqrt(2 * 365 * log(1 / (1 - p))));
}
string timeFrom12_30PM(double p) {
int minutes = findTime(p);
int totalMinutes = 30 + minutes - 1; // Adding minutes to 12:30 PM and -1 due to the presence of person at 12:30
int hours = 12 + totalMinutes / 60; // Calculate hours
int mins = totalMinutes % 60; // Calculate minutes
return to_string(hours) + ":" + (mins < 10 ? "0" : "") + to_string(mins);
}
int main() {
cout << "ARRIVAL TIME FROM 12:30 PM: " << timeFrom12_30PM(0.75) << endl;
return 0;
}
Short Explanation: This program determines the time a student should arrive to achieve a given probability of winning based on the birthday paradox.
findTime(double p)
:- Calculates the number of participants needed for the probability pp using the formula: n≈2⋅365⋅ln(11−p)n \approx \sqrt{2 \cdot 365 \cdot \ln\left(\frac{1}{1-p}\right)}
- Rounds up to the nearest integer.
timeFrom12_30PM(double p)
:- Adds the required minutes to 12:30 PM based on the number of participants.
- Adjusts the time to account for the first participant already present at 12:30 PM.
- Converts the total minutes to hours and minutes in a readable format.
main()
:- Calls the
timeFrom12_30PM
function with p=0.75p = 0.75 and prints the calculated arrival time.
- Calls the