Skip to content
Related Articles

Related Articles

Count of binary string of length N with at least X 0s and Y 1s

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 14 Dec, 2021

Given three numbers N, X, and Y, find the count of unique binary strings of length N having at least X 0s and Y 1s.

Examples:

Input: N=5, X=1, Y=2
Output: 25

Input: N=3, X=1, Y=1
Output: 6
Explanation: There are 3 binary strings of length 3 with at least 1 0s and 1 1s, such as: 001, 010, 100, 011, 101, 110

 

Naive approach: Generate all binary strings of length N and then count the number of strings with at least X 0s and Y 1s.
Time Complexity: O(2^N)
Auxiliary Space: O(1)

Better Approach: This problem can also be solved using Combinatorics. If the length is N, and given is X 0s, then there will be Y (=N-X) 1s. So we need to find the number of unique combinations for this, which can be obtained as (N)C(X) or (N)C(Y). Now for all unique binary strings, we need to find the nCi for values of i in the range [X, N-Y] and add it to a variable. The value of this sum after all iterations will be the required count.

Efficient Approach: The above approach can further be optimized with the help of the Pascal triangle to calculate nCr. Follow the steps below to solve the problem:

  • Initialize the 2-D array p[][] to calculate using the pascal triangle.
  • Initialize the variable sum as 0 to store the answer.
  • Iterate over the range [x, n-y] using the variable i and perform the following tasks:
    • Add the value p[n][i] to the variable sum.
  • After performing the above steps, print the value of sum as the answer.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
long long int p[31][31];
 
// Function to use pascal triangle
void pascalTriangle()
{
    p[0][0] = 1;
    p[1][0] = 1;
    p[1][1] = 1;
    for (int i = 2; i < 31; i++) {
        p[i][0] = 1;
        for (int j = 1; j < i; j++)
            p[i][j] = p[i - 1][j]
                      + p[i - 1][j - 1];
        p[i][i] = 1;
    }
}
 
// Function to count the total number of ways
long long int countWays(int n, int x, int y)
{
 
    // Store the answer
    long long int sum = 0;
 
    // Traverse
    for (long long int i = x; i <= n - y; i++) {
        sum += p[n][i];
    }
    return sum;
}
 
// Driver Code
int main()
{
    pascalTriangle();
 
    int N = 5, X = 1, Y = 2;
    cout << countWays(N, X, Y) << endl;
 
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
class GFG {
 
    static int[][] p = new int[31][31];
 
    // Function to use pascal triangle
    static void pascalTriangle()
    {
        p[0][0] = 1;
        p[1][0] = 1;
        p[1][1] = 1;
        for (int i = 2; i < 31; i++) {
            p[i][0] = 1;
            for (int j = 1; j < i; j++)
                p[i][j] = p[i - 1][j] + p[i - 1][j - 1];
            p[i][i] = 1;
        }
    }
 
    // Function to count the total number of ways
    static long countWays(int n, int x, int y)
    {
 
        // Store the answer
        long sum = 0;
 
        // Traverse
        for (int i = x; i <= n - y; i++) {
            sum += p[n][i];
        }
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        pascalTriangle();
 
        int N = 5;
        int X = 1;
        int Y = 2;
 
        System.out.println(countWays(N, X, Y));
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python program for the above approach
p = [[0 for i in range(31)] for i in range(31)]
 
# Function to use pascal triangle
def pascalTriangle():
    p[0][0] = 1
    p[1][0] = 1
    p[1][1] = 1
    for i in range(2, 31):
        p[i][0] = 1
        for j in range(1, i):
            p[i][j] = p[i - 1][j] + p[i - 1][j - 1]
        p[i][i] = 1
 
# Function to count the total number of ways
def countWays(n, x, y):
 
    # Store the answer
    sum = 0
 
    # Traverse
    for i in range(x, n - y + 1):
        sum += p[n][i]
    return sum
 
# Driver Code
pascalTriangle()
 
N = 5
X = 1
Y = 2
print(countWays(N, X, Y))
 
# This code is contributed by gfgking


C#




// C# code for the above approach
using System;
 
public class GFG {
 
    static int[,] p = new int[31,31];
 
    // Function to use pascal triangle
    static void pascalTriangle()
    {
        p[0,0] = 1;
        p[1,0] = 1;
        p[1,1] = 1;
        for (int i = 2; i < 31; i++) {
            p[i,0] = 1;
            for (int j = 1; j < i; j++)
                p[i,j] = p[i - 1,j] + p[i - 1,j - 1];
            p[i,i] = 1;
        }
    }
 
    // Function to count the total number of ways
    static long countWays(int n, int x, int y)
    {
 
        // Store the answer
        long sum = 0;
 
        // Traverse
        for (int i = x; i <= n - y; i++) {
            sum += p[n,i];
        }
        return sum;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        pascalTriangle();
 
        int N = 5;
        int X = 1;
        int Y = 2;
 
        Console.WriteLine(countWays(N, X, Y));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
    // JavaScript program for the above approach
 
    let p = new Array(31).fill(0).map(() => new Array(31).fill(0));
 
    // Function to use pascal triangle
    const pascalTriangle = () => {
        p[0][0] = 1;
        p[1][0] = 1;
        p[1][1] = 1;
        for (let i = 2; i < 31; i++) {
            p[i][0] = 1;
            for (let j = 1; j < i; j++)
                p[i][j] = p[i - 1][j]
                    + p[i - 1][j - 1];
            p[i][i] = 1;
        }
    }
 
    // Function to count the total number of ways
    const countWays = (n, x, y) => {
 
        // Store the answer
        let sum = 0;
 
        // Traverse
        for (let i = x; i <= n - y; i++) {
            sum += p[n][i];
        }
        return sum;
    }
 
    // Driver Code
 
    pascalTriangle();
 
    let N = 5, X = 1, Y = 2;
    document.write(countWays(N, X, Y));
 
// This code is contributed by rakeshsahni
 
</script>


Output

25

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!