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
Last Updated :
15 Jul, 2025
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++
// 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 possible
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;
}
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 possible
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
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 possible
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
C#
// C# program to implement the
// above approach
using System;
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 possible
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;
Console.Write(countStrings(N) + " ");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// javascript program to implement the
// above approach
// Function to calculate and returns the
// value of Binomial Coefficient C(n, k)
function binomialCoeff(n , k) {
var 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 (var 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
function countStrings(N) {
// If N is odd
if (N % 2 == 1)
// No such strings possible
return 0;
// Otherwise
else
{
N /= 2;
// Calculate value of 2nCn
var c = binomialCoeff(2 * N, N);
// Return 2nCn/(n+1)
return c / (N + 1);
}
}
// Driver code
var N = 6;
document.write(countStrings(N) + " ");
// This code is contributed by Princi Singh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of binary strings of length N having equal count of 0's and 1's Given an integer N, the task is to find the number of binary strings possible of length N having same frequency of 0s and 1s. If such string is possible of length N, print -1. Note: Since the count can be very large, return the answer modulo 109+7.Examples: Input: N = 2 Output: 2 Explanation: All po
6 min read
Check if all substrings of length K of a Binary String has equal count of 0s and 1s Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print âYesâ. Otherwise, print âNoâ. Examples: Input: S = "101010", K = 2Output: YesExplanation:Since all the substrings of length
6 min read
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' Given a binary string S, the task is to count total binary strings consisting of at least one '1' of length equal to the length of the given string after repeatedly removing all occurrences of substrings "10" and "00" from the given string. Examples: Input: S = "111"Output: 7Explanation: Since there
4 min read
Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's Given an integer N, the task is to find the number of Binary Strings of length N such that frequency of 1's is greater than the frequency of 0's. Example: Input: N = 2Output: 1Explanation: Count of binary strings of length 2 is 4 i.e. {"00", "01", "10", "11"}. The only string having frequency of 1's
8 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read
Generate all Binary Strings of length N with equal count of 0s and 1s Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1 Examples: Input: N = 2 Output: â01â, â10âExplanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s Input:
6 min read