Count of binary strings of length N having equal count of 0’s and 1’s and count of 1’s ≥ count of 0’s in each prefix substring

Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0‘s and 1‘s in which frequency of 1‘s are greater or equal to the frequency of 0‘s in every prefix substring.

Examples:

Input: N = 2
Output: 1
Explanation:
All possible binary strings of length 2 are {“00”, “01”, “10” and “11”}. 
Out of these 4 strings, only “01” and “10” have equal count of 0’s and 1’s.
Out of these two strings, only “10” contains more or equal numbers of 1’s than 0’s in every prefix substring.

Input: N = 4
Output: 2
Explanation : 
All possible binary strings of length 4, satisfying the required conditions are “1100” and “1010”.

Naive Approach: 
The simplest approach is to generate all the binary strings of length N and iterate each string to check if it contains an equal count of 0‘s and 1‘s and also check if the frequency of 1‘s is equal to greater than that of 0‘s in all its prefix substrings.
Time Complexity: O(N*2^N)
Auxiliary Space: O(1)
 



Efficient Approach: 
The above approach can be further optimized using the concept of Catalan Number. We just need to check the parity of N.

  • If N is an odd integer, frequencies of 0’s and 1’s cannot be equal. Therefore, the count of such required strings is 0.
  • If N is an even integer, the count of required substrings is equal to the (N/2)th Catalan number.

Illustration: 
N = 2 
Only possible string is “10“. Therefore, count is 1. 
N = 4 
Only possible strings are “1100” and “1010”. Therefore, count is 2. 
N = 6 
Only possible strings are “111000”, “110100”, “110010”, “101010” and “101100”. 
Therefore the count is 5. 
Hence, for each even value of N, it follows the sequence 1 2 5 14 …… 
Hence, the series is that of Catalan numbers. 
Therefore, it can be concluded that if N is an even integer, the count is equal to that of (N/2)th Catalan Number. 
 

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement 
// the above approach 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to calculate and returns the 
// value of Binomial Coefficient C(n, k) 
unsigned long int binomialCoeff(unsigned int n, 
                                unsigned 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 all 
// binary strings having equal count of 0's 
// and 1's and each prefix substring having 
// frequency of 1's >= frequencies of 0's 
unsigned long int countStrings(unsigned int N) 
    // If N is odd 
    if (N % 2 == 1) 
  
        // No such strings possibel 
        return 0; 
  
    // Otherwise 
    else
        N /= 2; 
  
        // Calculate value of 2nCn 
        unsigned long int
            = binomialCoeff(2 * N, N); 
  
        // Return 2nCn/(n+1) 
        return c / (N + 1); 
    
  
// Driver Code 
int main() 
    int N = 6; 
    cout << countStrings(N) << " "
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the
// above approach
import java.util.*;
  
class GFG{
  
// Function to calculate and returns the
// value of Binomial Coefficient C(n, k)
static long binomialCoeff(int n, int k)
{
    long 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 all 
// binary strings having equal count of 0's 
// and 1's and each prefix substring having 
// frequency of 1's >= frequencies of 0's 
static long countStrings(int N)
{
      
    // If N is odd
    if (N % 2 == 1)
  
        // No such strings possibel
        return 0;
  
    // Otherwise
    else 
    {
        N /= 2;
  
        // Calculate value of 2nCn
        long c = binomialCoeff(2 * N, N);
  
        // Return 2nCn/(n+1)
        return c / (N + 1);
    }
  
// Driver code
public static void main(String[] args)
{
    int N = 6;
      
    System.out.print(countStrings(N) + " ");
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to implement 
# the above approach 
  
# Function to calculate and returns 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 -
  
    # 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 all 
# binary strings having equal count of 0's 
# and 1's and each prefix substring having 
# frequency of 1's >= frequencies of 0's 
def countStrings(N): 
      
    # If N is odd 
    if (N % 2 == 1): 
  
        # No such strings possibel 
        return 0
  
    # Otherwise 
    else
        N //= 2
  
        # Calculate value of 2nCn 
        c= binomialCoeff(2 * N, N) 
  
        # Return 2nCn/(n+1) 
        return c // (N + 1
  
# Driver Code 
if __name__ == '__main__'
    N = 6
    print(countStrings(N)) 
  
# This code is contributed by Mohit Kumar 

chevron_right


Output: 

5

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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 : mohit kumar 29, offbeat