Open In App

# Maximizing Probability of one type from N containers

Given N containers consisting of N copies of number A and N copies of number B. We need to arrange the numbers in the N containers in such a way that the probability of choosing a container randomly and drawing one copy of number A is maximum.

Examples:

```Input : N = 1
Output : 0.5,

Input : N = 2
Output : 0.667```

We need to look for an optimal arrangement of the N copies of number A and N copies of number B in N containers.

```Pi = (Favorable Outcomes) / (Total Outcomes)

where Pi denotes the probability of an event i.```

So to maximize this, either we minimize the denominator (i.e., the total outcomes) or maximize the numerator (i.e., the total favorable Outcomes) keeping the other constant.

Consider a possible arrangement where in the first (N-1) containers we put only copies of Number A, such that first (N-1) containers contain (N-1) copies of the Number A and no copy of number B. The left out copy of number A and the N copies of number B are then put in the last container.

Probability (copy of A from any of the first (N-1) containers) = Pn-1 containers = 1
Probability (copy of A from Nth container) = PN = 1?(N+1)
Pmax = PN-1 containers * (N – 1) + PN
? Pmax = N / (N + 1)

## C++

 `// CPP program to find maximum probability of``// getting a copy 'A' from N``#include ``using` `namespace` `std;` `// Returns the Maximum probability for Drawing``// 1 copy of number A from N containers with N``// copies each of numbers A and B``double` `calculateProbability(``int` `N)``{``    ``// Pmax = N/(N+1)``    ``double` `probability = (``double``)N / (N + 1);``    ``return` `probability;``}` `int` `main()``{``    ``int` `N;``    ``double` `probabilityMax;` `    ``// 1. N = 1``    ``N = 1;``    ``probabilityMax = calculateProbability(N);``    ``cout << ``"Maximum Probability for N = "``         ``<< N << ``" is, "` `<< setprecision(4)``        ``<< fixed << probabilityMax << endl;` `    ``// 2. N = 2``    ``N = 2;``    ``probabilityMax = calculateProbability(N);``    ``cout << ``"Maximum Probability for N = "``         ``<< N << ``" is, "` `<< setprecision(4)``         ``<< fixed << probabilityMax << endl;` `    ``// 3. N = 10``    ``N = 10;``    ``probabilityMax = calculateProbability(N);``    ``cout << ``"Maximum Probability for N = "``         ``<< N << ``" is, "` `<< setprecision(4)``         ``<< fixed << probabilityMax << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find maximum probability of``// getting a copy 'A' from N``class` `GFG {``    ` `    ``// Returns the Maximum probability for Drawing``    ``// 1 copy of number A from N containers with N``    ``// copies each of numbers A and B``    ``static` `double` `calculateProbability(``int` `N)``    ``{``        ` `        ``// Pmax = N/(N+1)``        ``double` `probability = (``double``)N / (N + ``1``);``        ` `        ``return` `probability;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ` `        ``int` `N;``        ``double` `probabilityMax;` `        ``// 1. N = 1``        ``N = ``1``;``        ``probabilityMax = calculateProbability(N);``        ` `        ``System.out.println(``"Maximum Probability for"``                  ``+ ``" N = "` `+ N + ``" is, "` `+ Math.round(``                  ``probabilityMax * ``10000.0``) / ``10000.0``);` `        ``// 2. N = 2``        ``N = ``2``;``        ``probabilityMax = calculateProbability(N);``        ``System.out.println(``"Maximum Probability for N = "``                          ``+ N + ``" is, "` `+ Math.round(``                     ``probabilityMax * ``10000.0``) / ``10000.0``);` `        ``// 3. N = 10``        ``N = ``10``;``        ``probabilityMax = calculateProbability(N);``        ``System.out.println(``"Maximum Probability for N = "``                            ``+ N + ``" is, "` `+ Math.round(``                     ``probabilityMax * ``10000.0``) / ``10000.0``);``    ``}``}` `// This code is contributed by Anant Agarwal.`

## Python3

 `# Python3 program to find maximum``# probability of getting a copy 'A' from N` `# Returns the Maximum probability for``# Drawing 1 copy of number A from N``# containers with N copies each of``# numbers A and B``def` `calculateProbability(N):` `    ``# Pmax = N / (N+1)``    ``probability ``=` `N ``/` `(N ``+` `1``)``    ``return` `probability` `# Driver code` `# 1. N = 1``N ``=` `1``probabilityMax ``=` `calculateProbability(N)``print``(``"Maximum Probability for N = "``,``       ``N , ``"is, %.4f"` `%``probabilityMax)` `# 2. N = 2``N ``=` `2``probabilityMax ``=` `calculateProbability(N);``print``(``"Maximum Probability for N ="``,``       ``N, ``"is, %.4f"` `%``probabilityMax)` `# 3. N = 10``N ``=` `10``probabilityMax ``=` `calculateProbability(N);``print``(``"Maximum Probability for N ="``,``       ``N,``"is, %.4f"` `%``probabilityMax)` `# This code is contributed by Anant Agarwal.`

## C#

 `// C# program to find maximum probability of``// getting a copy 'A' from N``using` `System;` `class` `GFG {``    ` `    ``// Returns the Maximum probability for Drawing``    ``// 1 copy of number A from N containers with N``    ``// copies each of numbers A and B``    ``static` `double` `calculateProbability(``int` `N)``    ``{``        ` `        ``// Pmax = N/(N+1)``        ``double` `probability = (``double``)N / (N + 1);``        ``return` `probability;``    ``}``    ` `    ``//Driver code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `N;``        ``double` `probabilityMax;``     ` `        ``// 1. N = 1``        ``N = 1;``        ``probabilityMax = calculateProbability(N);``        ``Console.WriteLine(``"Maximum Probability for N = "``             ``+ N + ``" is, "` `+``        ``Math.Round(probabilityMax * 10000.0) / 10000.0);``     ` `        ``// 2. N = 2``        ``N = 2;``        ``probabilityMax = calculateProbability(N);``        ``Console.WriteLine(``"Maximum Probability for N = "``             ``+ N + ``" is, "` `+``        ``Math.Round(probabilityMax * 10000.0) / 10000.0);``     ` `        ``// 3. N = 10``        ``N = 10;``        ``probabilityMax = calculateProbability(N);``        ``Console.WriteLine(``"Maximum Probability for N = "``             ``+ N + ``" is, "` `+``        ``Math.Round(probabilityMax * 10000.0) / 10000.0);``     ` `    ``}``}` `// This code is contributed by Anant Agarwal.`

## PHP

 ``

## Javascript

 ``

Output:

```Maximum Probability for N = 1 is, 0.5000
Maximum Probability for N = 2 is, 0.6667
Maximum Probability for N = 10 is, 0.9091```

Time Complexity of the above program is O(1).