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}= 1Probability (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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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) ` `?> ` |

*chevron_right*

*filter_none*

**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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sequence vs Associative containers in C++
- Primitive data type vs. Object data type in Java with Examples
- Probability of Knight to remain in the chessboard
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 2
- Aptitude | Probability | Question 3
- Aptitude | Probability | Question 4
- Aptitude | Probability | Question 5
- Aptitude | Probability | Question 6
- Aptitude | Probability | Question 7
- Aptitude | Probability | Question 8
- Aptitude | Probability | Question 9
- Aptitude | Probability | Question 10
- Aptitude | Probability | Question 1
- QA - Placement Quizzes | Probability | Question 11
- QA - Placement Quizzes | Probability | Question 12
- QA - Placement Quizzes | Probability | Question 13
- QA - Placement Quizzes | Probability | Question 14
- QA - Placement Quizzes | Probability | Question 15
- Probability of a random pair being the maximum weighted pair

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.