# Drawing Red Out

Drawing Red Out is the third problem of the HackerRank Contest GOC-18. It is a medium label problem. The minimum requirement to solve this problem is the knowledge of probability, Bayes Theorem, and gcd. Time complexity is \( \mathcal O(1) \) for each test case.

Here in this problem, we are given two bags and each bag contains **RED** and **BLACK** balls. Let’s **BG1** and **BG2** represents two bags. Let’s also use **R** for RED balls and **B** for BLACK balls.

**P(BG1)**,** P(BG2)** is probability of selecting one of the bag which was given to be \( \frac{2}{5}\)and \( \frac{3}{5}\) respectively.** P(R | BG1)** denotes the probability of Drawing red ball, given that it is drawn from the first bag, Similarly for **P(R | BG2) **denotes the probability of Drawing red ball, given that it is drawn from the second bag. **P(BG2 | R)** is our desired probability.

Now from Baye’s Theorem we can write:

$$P(BG2 | R) = \frac {P(R|BG2) * P(BG2)}{(P(R|BG1) * P(BG1) + P(R|BG2) * P(BG2)}$$

$$P(R|BG2) = \frac{R2} {(R2+B2)}$$

$$P(R|BG1) = \frac{R1} {(R1+B1)}$$

Now we can reduce the above equation in following to write in \( \frac{P}{Q}\) form.

$$P = 3* R2 * (R1+B1) * (R2+B2)$$

$$Q = (R2+B2) * (3*R2*(R1+B1) + 2*R1*(R2+B2))$$

In order to write answer in **REDUCED** form of \( \frac{P}{Q}\). We can rewrite equation as:

$$ p = \frac{P}{GCD(p,Q)}$$

$$ q = \frac{Q}{GCD(p,Q)}$$

We provide C++ code for the above problem:

#include<bits/stdc++.h> using namespace std; int main() { int test; cin>>test; while(test--) { long r1, b1, r2, b2; cin>>r1>>b1>>r2>>b2; long P = 3 * r2 *( r1 + b1)*(r2+b2); long Q = (r2+b2)*(3*r2*(r1+b1) + 2*r1*(r2+b2)); //// __gcd(p, q) is inbuild function in C++ long p = P/__gcd(P, Q); long q = Q/__gcd(P, Q); cout<<p<<"/"<q<endl; } return 0; }

This editorial is contributed by Ravi Kant. If you like eLgo Academy and would like to contribute, you can mail your article to admin@elgoacademy.org. See your article appearing on the eLgo Academy page and help others. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.