📌  相关文章
📜  长度为N的二进制字符串的计数,以使1的频率超过0的频率

📅  最后修改于: 2021-06-25 17:45:38             🧑  作者: Mango

给定整数N ,任务是找到长度为N的二进制字符串数,以使1的频率大于0的频率。

例子:

天真的方法:解决此问题的最简单方法是生成所有长度为N的二进制字符串,并对每个字符串进行迭代以找到1和0的频率。如果1的频率大于0的频率,则增加计数器。最后,打印计数。

时间复杂度: O(N * 2 N )
辅助空间: O(1)

高效方法:要观察上述方法,需要进行以下观察:

  1. 对于S 1中的每个字符串, S 0中都存在一个字符串
    假设“ 1110 ”是S 1中的字符串,则其在S 0中的对应字符串将是“ 0001 ”。同样,对于S 1中的每个字符串 S 0中存在一个字符串。因此, S 1 = S 0 (每N个)。
  2. 如果N奇数,S等于0
  3. 如果N偶数,S等于C(N,N / 2)

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate
// and return the value of
// Binomial Coefficient C(n, k)
unsigned long int binomialCoeff(unsigned long int n,
                                unsigned long int k)
{
 
    unsigned long int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to return the count of
// binary strings of length N such
// that frequency of 1's exceed that of 0's
unsigned long int countOfString(int N)
{
    // Count of N-length binary strings
    unsigned long int Stotal = pow(2, N);
 
    // Count of N- length binary strings
    // having equal count of 0's and 1's
    unsigned long int Sequal = 0;
 
    // For even length strings
    if (N % 2 == 0)
        Sequal = binomialCoeff(N, N / 2);
 
    unsigned long int S1 = (Stotal - Sequal) / 2;
    return S1;
}
 
// Driver Code
int main()
{
    int N = 3;
    cout << countOfString(N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate
// and return the value of
// Binomial Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to return the count of
// binary Strings of length N such
// that frequency of 1's exceed that of 0's
static int countOfString(int N)
{
     
    // Count of N-length binary Strings
    int Stotal = (int) Math.pow(2, N);
 
    // Count of N- length binary Strings
    // having equal count of 0's and 1's
    int Sequal = 0;
 
    // For even length Strings
    if (N % 2 == 0)
        Sequal = binomialCoeff(N, N / 2);
 
    int S1 = (Stotal - Sequal) / 2;
    return S1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    System.out.print(countOfString(N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to calculate
# and return the value of
# Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
 
    res = 1
 
    # Since C(n, k) = C(n, n-k)
    if(k > n - k):
        k = n - k
 
    # Calculate the value of
    # [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
# Function to return the count of
# binary strings of length N such
# that frequency of 1's exceed that of 0's
def countOfString(N):
 
    # Count of N-length binary strings
    Stotal = pow(2, N)
 
    # Count of N- length binary strings
    # having equal count of 0's and 1's
    Sequal = 0
 
    # For even length strings
    if(N % 2 == 0):
        Sequal = binomialCoeff(N, N // 2)
 
    S1 = (Stotal - Sequal) // 2
 
    return S1
 
# Driver Code
N = 3
 
print(countOfString(N))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to calculate
// and return the value of
// Binomial Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to return the count of
// binary Strings of length N such
// that frequency of 1's exceed that of 0's
static int countOfString(int N)
{
     
    // Count of N-length binary Strings
    int Stotal = (int) Math.Pow(2, N);
 
    // Count of N- length binary Strings
    // having equal count of 0's and 1's
    int Sequal = 0;
 
    // For even length Strings
    if (N % 2 == 0)
        Sequal = binomialCoeff(N, N / 2);
 
    int S1 = (Stotal - Sequal) / 2;
    return S1;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    Console.Write(countOfString(N));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)