Airplane Seat Assignment Probability

n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of the passengers will:

1. Take their own seat if it is still available, 

2. Pick other seats randomly when they find their seat occupied 

What is the probability that the n-th person can get his own seat?

 Example:

Input: n = 2
Output: 0.50000
Explanation: The second person has a probability of 0.5 to get the second seat (when first person gets the first seat).

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

double nthPersonGetsNthSeat(int n)
{
    if (n == 1)
        return 1.0;

    return 0.5;
}

int main()
{
    int n = 2;

    cout << nthPersonGetsNthSeat(n);

    return 0;
}


No comments:

Post a Comment