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 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) = 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : manishshaw1