You are given a function foo() that represents a biased coin. When foo() is called, it returns 0 with 60% probability, and 1 with 40% probability. Write a new function that returns 0 and 1 with 50% probability each. Your function should use only foo(), no other library method.
We know foo() returns 0 with 60% probability. How can we ensure that 0 and 1 are returned with 50% probability?
The solution is similar to this post. If we can somehow get two cases with equal probability, then we are done. We call foo() two times. Both calls will return 0 with 60% probability. So the two pairs (0, 1) and (1, 0) will be generated with equal probability from two calls of foo(). Let us see how.
(0, 1): The probability to get 0 followed by 1 from two calls of foo() = 0.6 * 0.4 = 0.24
(1, 0): The probability to get 1 followed by 0 from two calls of foo() = 0.4 * 0.6 = 0.24
So the two cases appear with equal probability. The idea is to return consider only the above two cases, return 0 in one case, return 1 in other case. For other cases [(0, 0) and (1, 1)], recur until you end up in any of the above two cases.
The below program depicts how we can use foo() to return 0 and 1 with equal probability.
This article is compiled by Shashank Sinha and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Coin Change | DP-7
- Frobenius coin problem
- Coin Change | BFS Approach
- Find the player who will win the Coin game
- Probability of getting K heads in N coin tosses
- Probability of not getting two consecutive heads together in N tosses of coin
- Expected number of coin flips to get two heads in a row?
- Count of total Heads and Tails after N flips in a coin
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Make A, B and C equal by adding total value N to them
- Number of continuous reductions of A from B or B from A to make them (1, 1)
- Make all numbers of an array equal
- Minimum removals to make array sum odd
- Check if it is possible to make x and y zero at same time with given operation
- Make the array non-decreasing with the given operation
- Print steps to make a number in form of 2^X - 1
- Make all elements of an array equal with the given operation
- Minimum number to be added to all digits of X to make X > Y
- Possible to make a divisible by 3 number using all digits in an array