
- 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
Maximizing Probability of one type from N containers in C++
Probability Pi= (No. of Favourable Outcomes) / (Total no. of Outcomes).
Given is a number N which is the number of containers present. And we have N copies of two numbers X and Y. The task is to divide copies of one number X into N containers such that the probability of drawing a copy of X is maximum. From above it can be seen that to maximize Pi, we can either maximize the numerator ( No. of favourable outcomes) or minimize the denominator(Total no. of Outcomes). This can be done in a manner that only one container has a copy of Y and all containers have copies of X. N-1 containers have a copy of X each ( N-1 copies of X). And 1 container has 1 copy of Y and N copies of Y.
Probability (copy of X from first (n-1) containers) = Pn-1= 1
Probability (copy of X from last container) = Pn = 1/(n+1)
Pm = Pn-1 * (n – 1) + Pn ∴ Pm = n / (n + 1)
Input− N=1
Output− Maximum probability for N=1 is 0.5
Explanation − As there is only 1 container and 1 copy of X and Y each in it. Maximum probability of drawing X is 0.5.
Input− N=3
Output− Maximum probability for N=1 is 0.75
Explanation − Here All containers have 1 copy of X and the last one has all 3 copies of Y.
Approach used in the below program is as follows
Input an integer value for N that is the number of containers.
Declare a variable to store the maximum probability of X say maxP.
For given N calculate maxP as N/(N+1).
Example
#include <bits/stdc++.h> using namespace std; int main(){ int N=3; double maxP = (double)N / (N + 1); cout << "Maximum Probability for N = " << N << " is, " <<maxP << endl; return 0; }
Output
If we run the above code it will generate the following output −
Maximum Probability for N = 3 is, 0.75
- Related Articles
- Maximizing Unique Pairs from two arrays in C++
- Swapping of subranges from different containers in C++
- Find the probability of reaching all points after N moves from point N in C++
- Probability of rain on N+1th day in C++
- Find n-variables from n sum equations with one missing in C++
- Maximizing array sum with given operation in C++
- Evolution of Docker from Linux Containers
- Containers in C++ STL
- Probability of getting at least K heads in N tosses of Coins in C++
- Convert variables from one type to another in Arduino
- Probability of getting a sum on throwing 2 Dices N times in C++
- Connecting From Docker Containers to Resources in Host
- Maximizing the elements with a[i+1] > a[i] in C++
- Certify the type of motions in a, b, c, & d."\n
- Probability that the pieces of a broken stick form a n sided polygon in C++
