Given a value X, the task is to find a minimum size binary string, such that if any 2 characters are deleted at random, the probability that both the characters will be ‘1’ is 1/X. Print the size of such binary string.
Input: X = 2
Let the binary string be “0111”.
Probability of choosing 2 1s from given string is = 3C2 / 4C2 = 3/6 = 1/2 (which is equal to 1/X).
Hence the required size is 4.
(Any 4 size binary string with 3 ‘1’s and 1 ‘0’ can be taken for this example).
Input: X = 8
Approach: We will try to find a formula to solve this problem.
r = Number of 1’s in the string
b = Number of 0’s in the string.
- If two characters are deleted at random, then
Total number of ways = (r + b) C 2.
- If 2 characters are desired to be 1’s, Favourable number of cases = r C 2.
- Hence, P(both are 1’s) = rC2 / (r + b)C2.
- A tricky observation to further proceed our calculation is:
- Squaring the inequality and comparing with the equality, we get
- If r > 1, we take square root on all 3 sides.
- Taking the leftmost part of the inequality, we get:
- Similarly, taking the rightmost part of the inequality, we get:
- Combining the derived conclusions, we get the range of r in terms of b.
- For the minimum value of string, we set b = 1
- In order to get a valid minimum r, we take the first integer value of r in this range.
For Example: if X = 2
Hence, r = 3 and b = 1.
P(both character are 1’s) = 3C2 / 4C2 = 2/4 = 1/2
Below is the implementation of the above approach.
Time Complexity: O(1), as the difference between left_lim and right_lim will be always less than 1.
Auxiliary Space: O(1)
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to keep all 1s together in a Binary string
- Minimum number of operations required to sum to binary string S
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to convert one binary string to another
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum number of operations required to obtain a given Binary String
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum number of given operations required to convert a string to another string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum time required to cover a Binary Array
- Minimum flip required to make Binary Matrix symmetric
- Minimum adjacent swaps required to Sort Binary array
- Minimum changes required such that the string satisfies the given condition
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Select a Random Node from a tree with equal probability
- Probability of choosing a random pair with maximum sum in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : SHIVAMSINGH67