# 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.

P_{i}= (Favorable Outcomes) / (Total Outcomes) where P_{i}denotes the probability of an event i.

So to maximize this, either we minimize the denominator (i.e., the total outcomes) or maximise 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) =

P_{n-1 containers}= 1

Probability (copy of A from N^{th}container) =P_{N}=^{1}⁄_{(N+1)}

P_{max}= P_{N-1 containers}* (N – 1) + P_{N}∴ P_{max}= N / (N + 1)

## C++

`// CPP program to find maximum probability of` `// getting a copy 'A' from N` `#include <bits/stdc++.h>` `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

`<?php` `// PHP 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` `function` `calculateProbability(` `$N` `)` `{` ` ` `// Pmax = N/(N+1)` ` ` `$probability` `= ` `$N` `/ (` `$N` `+ 1);` ` ` `return` `$probability` `;` `}` `// 1. N = 1` `$N` `= 1;` `$probabilityMax` `= calculateProbability(` `$N` `);` `echo` `(` `"Maximum Probability for N = "` `.` ` ` `$N` `. ` `" is, "` `.` ` ` `round` `(` `$probabilityMax` `, 4). ` `"\n"` `);` `// 2. N = 2` `$N` `= 2;` `$probabilityMax` `= calculateProbability(` `$N` `);` `echo` `(` `"Maximum Probability for N = "` `.` ` ` `$N` `. ` `" is, "` `.` ` ` `round` `(` `$probabilityMax` `, 4) . ` `"\n"` `);` `// 3. N = 10` `$N` `= 10;` `$probabilityMax` `= calculateProbability(` `$N` `);` `echo` `(` `"Maximum Probability for N = "` `.` ` ` `$N` `. ` `" is, "` `.` ` ` `round` `(` `$probabilityMax` `, 4) . ` `"\n"` `);` ` ` `// This code is contributed by` `// Manish Shaw(manishshaw1)` `?>` |

## Javascript

`<script>` `// JavaScript 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` `function` `calculateProbability(N)` `{` ` ` ` ` `// Pmax = N/(N+1)` ` ` `let probability = N / (N + 1);` ` ` ` ` `return` `probability;` `}` `// Driver Code` `let N;` `let probabilityMax;` `// 1. N = 1` `N = 1;` `probabilityMax = calculateProbability(N);` ` ` `document.write(` `"Maximum Probability for"` `+` ` ` `" N = "` `+ N + ` `" is, "` `+ Math.round(` ` ` `probabilityMax * 10000.0) / 10000.0 + ` `"<br/>"` `);` `// 2. N = 2` `N = 2;` `probabilityMax = calculateProbability(N);` `document.write(` `"Maximum Probability for N = "` `+ N +` ` ` `" is, "` `+ Math.round(` ` ` `probabilityMax * 10000.0) / 10000.0 + ` `"<br/>"` `);` `// 3. N = 10` `N = 10;` `probabilityMax = calculateProbability(N);` `document.write(` `"Maximum Probability for N = "` `+` ` ` `N + ` `" is, "` `+ Math.round(` ` ` `probabilityMax * 10000.0) / 10000.0 + ` `"<br/>"` `);` ` ` `// This code is contributed by avijitmondal1998` `</script>` |

**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).

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**