给出的值X,该任务是找到最小尺寸二进制字符串,使得如果任何2字符被随机删除,这两个字符将是“1”的概率为1 / X。打印此类二进制字符串的大小。
例子:
Input: X = 2
Output: 4
Explanation:
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
Output: 5
方法:我们将尝试找到解决该问题的公式。
Let
r = Number of 1’s in the string
and
b = Number of 0’s in the string.
- 如果随机删除两个字符,则
Total number of ways = (r + b) C 2.
- 如果希望2个字符为1,则例数= r C 2 。
- 因此,P(均为1)= r C2 /(r + b) C2 。
- 进一步进行计算的棘手观察是:
- 平方不等式并与等式进行比较,我们得到
- 如果r> 1,我们在所有3边取平方根。
- 取不等式最左边的部分,我们得到:
- 同样,考虑不平等的最右边部分,我们得到:
- 结合得出的结论,我们得到以b表示的r的范围。
- 对于字符串的最小值,我们设置b = 1
- 为了获得有效的最小值r,我们取该范围内r的第一个整数值。
For Example: if X = 2
Hence, r = 3 and b = 1.
P(both character are 1’s) = 3C2 / 4C2 = 2/4 = 1/2
下面是上述方法的实现。
C++
// C++ implementation of the
// above approach
#include
using namespace std;
// Function returns the minimum
// size of the string
int MinimumString(int x)
{
// From formula
int b = 1;
// Left limit of r
double left_lim = sqrt(x) + 1.0;
// Right limit of r
double right_lim = sqrt(x) + 2.0;
int r;
for (int i = left_lim; i <= right_lim; i++) {
if (i > left_lim and i < right_lim) {
// Smallest integer in
// the valid range
r = i;
break;
}
}
return b + r;
}
// Driver Code
int main()
{
int X = 2;
cout << MinimumString(X);
return 0;
}
Java
// Java implementation of the
// above approach
import java.util.*;
class GFG{
// Function returns the minimum
// size of the String
static int MinimumString(int x)
{
// From formula
int b = 1;
// Left limit of r
double left_lim = Math.sqrt(x) + 1.0;
// Right limit of r
double right_lim = Math.sqrt(x) + 2.0;
int r = 0;
for(int i = (int)left_lim; i <= right_lim; i++)
{
if (i > left_lim && i < right_lim)
{
// Smallest integer in
// the valid range
r = i;
break;
}
}
return b + r;
}
// Driver Code
public static void main(String[] args)
{
int X = 2;
System.out.print(MinimumString(X));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of
# the above approach
from math import sqrt
# Function returns the minimum
# size of the string
def MinimumString(x):
# From formula
b = 1
# Left limit of r
left_lim = sqrt(x) + 1.0
# Right limit of r
right_lim = sqrt(x) + 2.0
for i in range(int(left_lim),
int(right_lim) + 1):
if(i > left_lim and i < right_lim):
# Smallest integer in
# the valid range
r = i
break
return b + r
# Driver Code
if __name__ == '__main__':
X = 2
print(MinimumString(X))
# This code is contributed by Shivam Singh
C#
// C# implementation of the
// above approach
using System;
class GFG{
// Function returns the minimum
// size of the String
static int MinimumString(int x)
{
// From formula
int b = 1;
// Left limit of r
double left_lim = Math.Sqrt(x) + 1.0;
// Right limit of r
double right_lim = Math.Sqrt(x) + 2.0;
int r = 0;
for(int i = (int)left_lim; i <= right_lim; i++)
{
if (i > left_lim && i < right_lim)
{
// Smallest integer in
// the valid range
r = i;
break;
}
}
return b + r;
}
// Driver Code
public static void Main(String[] args)
{
int X = 2;
Console.Write(MinimumString(X));
}
}
// This code is contributed by gauravrajput1
Javascript
4
时间复杂度: O(1),因为left_lim和right_lim之差始终小于1。
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。