Probability of getting more value in third dice throw

Given that the three players playing a game of rolling a dice. Player1 rolled a dice got A and player2 rolled a dice got B. The task is to find the probability of player3 to win the match and Player3 wins if he gets more than both of them.

Examples:

Input: A = 2, B = 3
Output: 1/2
Player3 wins if he gets 4 or 5 or 6

Input: A = 1, B = 2
Output: 2/3
Player3 wins if he gets 3 or 4 or 5 or 6


Approach: The idea is to find the maximum of A and B and then 6-max(A, B) gives us remaining numbers C should get to win the match. So, one can find the answer dividing 6-max(A, B) and 6 with GCD of these two.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find probability to C win the match
#include <bits/stdc++.h>
using namespace std;
  
// function to find probability to C win the match
void Probability(int A, int B)
{
    int C = 6 - max(A, B);
  
    int gcd = __gcd(C, 6);
  
    cout << C / gcd << "/" << 6 / gcd;
}
  
// Driver code
int main()
{
    int A = 2, B = 4;
  
    // function call
    Probability(A, B);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//  Java program to find probability to C win the match
  
import java.io.*;
  
class GFG {// Recursive function to return gcd of a and b 
    static int __gcd(int a, int b) 
    
        // Everything divides 0  
        if (a == 0
          return b; 
        if (b == 0
          return a; 
         
        // base case 
        if (a == b) 
            return a; 
         
        // a is greater 
        if (a > b) 
            return __gcd(a-b, b); 
        return __gcd(a, b-a); 
    }
      
    
// function to find probability to C win the match
static void Probability(int A, int B)
{
    int C = 6 - Math.max(A, B);
  
    int gcd = __gcd(C, 6);
  
    System.out.print( C / gcd + "/" + 6 / gcd);
}
  
// Driver code
  
    public static void main (String[] args) {
    int A = 2, B = 4;
  
    // function call
    Probability(A, B);
    }
}
// This code is contributed by shs..

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find probability
# to C win the match
  
# import gcd() from math lib.
from math import gcd
  
# function to find probability 
# to C win the match
def Probability(A, B) :
    C = 6 - max(A, B)
      
    __gcd = gcd(C, 6)
      
    print(C // __gcd, "/", 6 // __gcd)
  
# Driver Code
if __name__ == "__main__" :
      
    A, B = 2, 4
  
    # function call
    Probability(A, B)
      
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find probability 
// to C win the match 
using System;
  
class GFG
{
      
// Recursive function to return 
// gcd of a and b 
static int __gcd(int a, int b) 
    // Everything divides 0 
    if (a == 0) 
        return b; 
    if (b == 0) 
        return a; 
      
    // base case 
    if (a == b) 
        return a; 
      
    // a is greater 
    if (a > b) 
        return __gcd(a - b, b); 
    return __gcd(a, b - a); 
  
// function to find probability 
// to C win the match 
static void Probability(int A, int B) 
    int C = 6 - Math.Max(A, B); 
  
    int gcd = __gcd(C, 6); 
  
    Console.Write(C / gcd + "/" + 6 / gcd); 
  
// Driver code 
static public void Main ()
{
    int A = 2, B = 4; 
      
    // function call 
    Probability(A, B); 
  
// This code is contributed by ajit. 

chevron_right


PHP

Output:

1/3


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.