Java solution to maximize the chances of winning in Monty-Hall Paradox Problem

In this Java Tutorial, we are going to solve the maximum chance of winning the famous Monty-Hall Paradox Problem in Java.

Maximize the chances of winning in Monty Hall Paradox in Java

We will learn how to maximize the chances of winning in Monty hall paradox in Java. Hope you gonna love it. Before jumping to the code snippet let’s take a look at the basic information. You may skip this portion if you wish.

What is Monty-Hall Paradox?

It is a brain-teaser, in the form of a probability puzzle, based on an American Television game show ” Let’s make a deal ” and named after its original host ” Monty-Hall”.

Let’s discuss the problem :

Suppose you are on a game show, and you are given the choice of three doors:- Behind one door is a car; behind the other goats. You pick a door, and the host who knows what’s behind the doors opens another door which has a goat. Then he says to you ” Do you want to switch your choice”?

In the problem, we have to print a variable ‘a’  which is the initial choice of the contestant. Now we are reading another variable ‘b’ which is the door opened by the host and it has a goat. Now, we have to print a third variable ‘c’ whose value is equal to ‘a’ if the contestant sticks to his first choice or if the contestant switches his choice then ‘c’ will be the remaining value between 1 to 3.  We have to print the value of ‘c’ in such a way that chance of winning a car is maximum

Constraint :- 1<=a<=3 , 1<=b<=3 , 1<=c<=3 ;

 Is it advantageous to switch the choice by a contestant in Monty hall paradox

Yes, to maximize the chances of winning a car contestant must switch to another door. If contestant switches he has 2/3 chances to win a car, while if he sticks on his first choice will have 1/3 chances of winning a car.

Algorithm To Solve Monty hall paradox

Basic, algorithm behind this problem is to choose a value for the first choice of contestant as we have chosen ‘a’ in the code below, then a value %3 (modulo 3 is used such that value no goes beyond limit) for gate opened by host then as we know there are three gates and sum of (1,2,3) is 6 so value of 3rd gate chosen by contestant must be(6-(sum of value of the first choice of contestant and value of gate opened by host ) ) to maximize the chances of winning of car .

You may also learn,

 

Code to Maximize The Chance of winning in Monty-Hall Paradox Problem in Java

import java.util.Scanner;

public class Codespeedy
{
 public static void main(String[] args)
 {
      long a,b,c;
      
      Scanner sc = new Scanner(System.in);

    a=sc.nextLong();
      
    b=(a+1)%3;
    
        System.out.println("Initial Choice of Contestant");
    System.out.println(a);
    
    System.out.println("Door opened by host :- which has a goat");
      b=sc.nextLong();
      System.out.println(b);
    
    
    System.out.println("Final Choice of Contestant");
     System.out.println(6-(a+b));
    
  }
}

 

OUTPUT

Initial Choice of Contestant
1
Door opened by host :- which has a goat
2
Final Choice of Contestant
3

So, in the example taken contestant has first chosen gate 1. Then the host opened gate 2 which has a goat. Now if contestant remains to stick to his choice of gate 1 he has 33./. of winning chance only, but if he switches the choices he has 66./. of winning chances. So, to maximize the chances of winning contestant switches his choice and finally, moves to gate 3.

You may also learn,

One response to “Java solution to maximize the chances of winning in Monty-Hall Paradox Problem”

  1. Akshat says:

    Nice explained

Leave a Reply

Your email address will not be published. Required fields are marked *