Probability of getting more value in third dice throw
Last Updated :
11 Jul, 2022
Given that the three players playing a game of rolling dice. Player1 rolled a die got A and player2 rolled a die 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 the 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++
#include <bits/stdc++.h>
using namespace std;
void Probability( int A, int B)
{
int C = 6 - max(A, B);
int gcd = __gcd(C, 6);
cout << C / gcd << "/" << 6 / gcd;
}
int main()
{
int A = 2, B = 4;
Probability(A, B);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
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);
}
public static void main (String[] args) {
int A = 2 , B = 4 ;
Probability(A, B);
}
}
|
Python 3
from math import gcd
def Probability(A, B) :
C = 6 - max (A, B)
__gcd = gcd(C, 6 )
print (C / / __gcd, "/" , 6 / / __gcd)
if __name__ = = "__main__" :
A, B = 2 , 4
Probability(A, B)
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
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);
}
static public void Main ()
{
int A = 2, B = 4;
Probability(A, B);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function Probability( $A , $B )
{
$C = 6 - max( $A , $B );
$gcd = __gcd( $C , 6);
echo ( $C / $gcd ) . "/" .
(6 / $gcd );
}
$A = 2;
$B = 4;
Probability( $A , $B );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function Probability(A, B)
{
let C = 6 - Math.max(A, B);
let gcd = __gcd(C, 6);
document.write(parseInt(C / gcd, 10) + "/" + parseInt(6 / gcd, 10));
}
let A = 2, B = 4;
Probability(A, B);
</script>
|
Time Complexity: O(log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))
Share your thoughts in the comments
Please Login to comment...