Showing posts with label Puzzle. Show all posts
Showing posts with label Puzzle. Show all posts

Thursday, 29 June 2017

To Maximize the Probability of Red Marble

You have 50 red balls and 50 blue balls, you have to place these balls in two containers in such a way that probability of a person picking up a red ball from any container is maximum.

1. For container 1,
You should have to place one red marble into 1st Container. By this when we select this Container 1 P(Red|C1) will be 1.

2.  For container 2,
Rest all marbles should have to place into 2nd container. By this when we select this Container 2 P(Red|C2) will be 49/99.

Here, P(C1) = P(C2) = 1/2 as there is only 2 containers.

So answer will be P(Red) = P(C1)*P(Red|C1) + P(C2)*(Red|C2)
                                                  P(Red) = (1/2)*(1) + (1/2)*(49/99)
                                                  P(Red) = 0.747474.

Proof of above given solution using the Java code:
public class MaximizeRedBallProbability {
     /**
      * This is the code which maximize the probability.
      * @param red
      * @param blue
      * @return probability
      */
     private static double getProbability(int red, int blue) {
           double max=0;
           int maxRed = 0;
           int maxBlue = 0;
          
           for (int i = 1; i < red; i++) {
                for (int j = 0; j < blue; j++) {
                     double prb = (0.5*i)/(i+j) + (0.5*(50-i))/(100-i-j);
                    
                     if (prb>max) {
                           max = prb;
                       maxRed=i;
                       maxBlue=j;
                     }
                }
           }
           System.out.println("Container 1, Red balls: "+maxRed +" Blue balls : "+maxBlue);
           System.out.println("Container 2, Red balls: "+(red-maxRed) +" Blue balls : "+(blue-maxBlue));
           return max;
     }
    
     /** Driver Method. */
     public static void main(String[] args) {
           int red = 50;
           int blue = 50;
           double p = getProbability(red,blue);
           System.out.println("Maximum probability of RED ball "+p);
     }
}
Output: 7474747474747475

                                                  
Related Posts Plugin for WordPress, Blogger...