
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Airplane Seat Assignment Probability in C++
Suppose n passengers board an airplane with exactly n seats. If the first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will follow these operations −
Take their own seat written in the ticket if it is still available,
Pick other seats randomly when they find their seat occupied
So we have to find what is the probability that the n-th person can get his own seat? So if the input is 2, then the output will be 0.5. So the second person has a probability of 0.5 to get the second seat (when first person gets the first seat).
To solve this, we will follow these steps −
If n is 1, then return 1, otherwise 0.5
Example (C++)
Let us see the following implementation to get a better understanding −
class Solution { public: double nthPersonGetsNthSeat(int n) { if (n == 1) return 1; return 0.5; } };
Input
2
Output
0.50000
- Related Articles
- Program to find probability of getting assigned seat for the last person in an airplane after seat shuffling in Python
- Program to implement seat reservation manager in Python
- Assignment Operators in C++
- How to Make a Paper Airplane?
- Compound Assignment Operators in C++
- Compound assignment operators in C#
- De-structuring assignment in JavaScript.
- Short Circuit Assignment in JavaScript
- Assignment operators in Dart Programming
- Passing the Assignment in C++
- Python Assignment Operators
- Java Assignment Operators
- Perl Assignment Operators
- Airplane Ear: Causes, Symptoms, Diagnosis, and Treatment
- What are Assignment Operators in JavaScript?

Advertisements