Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Probability of getting more value in third dice throw
Next article icon

Probability of getting more value in third dice throw

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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; } 
Java
 //  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.. 
Python 3
# 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 
C#
// 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.  
JavaScript
<script>     // Javascript program to find probability      // to C win the match           // Recursive function to return      // gcd of a and b      function __gcd(a, 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      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;             // function call      Probability(A, B);           </script> 
PHP
<?php // PHP program to find probability  // to C win the match  // Find gcd() function __gcd($a, $b)  {      if ($b == 0)          return $a;      return __gcd($b, $a % $b);  }  // function to find probability  // to C win the match function Probability($A, $B) {     $C = 6 - max($A, $B);      $gcd = __gcd($C, 6);      echo ($C / $gcd) . "/" .                   (6 / $gcd); }  // Driver code $A = 2; $B = 4;  // function call Probability($A, $B);  // This code is contributed by mits ?> 

Output
1/3

Time Complexity: O(log(min(a, b))), where a and b are two parameters of gcd.

Auxiliary Space: O(log(min(a, b)))


Next Article
Probability of getting more value in third dice throw

P

pawan_asipu
Improve
Article Tags :
  • Mathematical
  • Competitive Programming
  • Computer Science Fundamentals
  • DSA
  • Probability
Practice Tags :
  • Mathematical

Similar Reads

    Probability of getting all possible values on throwing N dices
    Given an integer N denoting the number of dices, the task is to find the probability of every possible value that can be obtained by throwing N dices together. Examples: Input: N = 1 Output: 1: 0.17 2: 0.17 3: 0.17 4: 0.17 5: 0.17 6: 0.17 Explanation: On throwing a dice, the probability of all value
    7 min read
    Probability of getting a sum on throwing 2 Dices N times
    Given the sum. The task is to find out the probability of occurring that sum on the thrown of the two dice N times. Probability is defined as the favorable numbers of outcomes upon total numbers of the outcome. Probability always lies between 0 and 1.Examples: Input: sum = 11, times = 1 Output: 2 /
    7 min read
    Probability of winning in a Die-throw game
    Given that 2 players are playing a die-throw game. The game is one player throws a die and he got a point, he can move forward accordingly to the point. All occurrences to get the point have equal probability. Let player1 start a point x and player2 start a point y. Player 1 can receive a point up t
    8 min read
    What is the probability of getting a 2 or a 5 when a die is rolled?
    Probability is the estimation of the possibility of random events happening, and its value ranges from 0 to 1. The probability of a sure event is always one, and the event that will never occur has a probability of zero. You may have also wondered how weather stations predict that it will rain today
    4 min read
    Probability of hitting the target Nth time at Mth throw
    Given integers N, M and p, the task is to find the probability of hitting a target for the Nth time at the Mth throw where p is the probability of hitting the target. Examples: Input: N = 1, M = 2, p = 0.3Output: 0.21Explanation: The target can be hit for the first time in the second throw only when
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences