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 = 2Output:1Explanation:

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” containsmore or equal numbers of 1’s than0’s in every prefix substring.

Input:N = 4Output:2Explanation :

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 aninteger, frequencies of 0’s and 1’s cannot be equal. Therefore, the count of such required strings is**odd****0**. - If
**N**is aninteger, the count of required substrings is equal to the**even**.**(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 eachvalue of N, it follows the sequence 1 2 5 14 ……even

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++

`// 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` `c ` ` ` `= 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*

*filter_none*

## Java

`// 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*

*filter_none*

## Python3

`# 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 ` `-` `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 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*

*filter_none*

**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.

## Recommended Posts:

- Count of binary strings of length N having equal count of 0's and 1's
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count number of binary strings of length N having only 0's and 1's
- Count of binary strings of given length consisting of at least one 1
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Check if count of Alphabets and count of Numbers are equal in the given String
- Minimum count of Full Binary Trees such that the count of leaves is N
- Count of sub-strings of length n possible from the given string
- Count of sub-strings with equal consecutive 0's and 1's
- Count of non-palindromic strings of length M using given N characters
- Count the number of Special Strings of a given length N
- Count ways to increase LCS length of two strings by one
- Count of same length Strings that exists lexicographically in between two given Strings
- Count the number of subsequences of length k having equal LCM and HCF
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Maximum count of sub-strings of length K consisting of same characters
- Count of unique palindromic strings of length X from given string
- Maximum number of set bits count in a K-size substring of a Binary String
- Count of Binary Strings possible as per given conditions

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.