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++
#include <bits/stdc++.h>
using namespace std;
unsigned long int binomialCoeff(unsigned int n,
unsigned int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int countStrings(unsigned int N)
{
if (N % 2 == 1)
return 0;
else {
N /= 2;
unsigned long int c
= binomialCoeff(2 * N, N);
return c / (N + 1);
}
}
int main()
{
int N = 6;
cout << countStrings(N) << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static long binomialCoeff( int n, int k)
{
long res = 1 ;
if (k > n - k)
k = n - k;
for ( int i = 0 ; i < k; ++i)
{
res *= (n - i);
res /= (i + 1 );
}
return res;
}
static long countStrings( int N)
{
if (N % 2 == 1 )
return 0 ;
else
{
N /= 2 ;
long c = binomialCoeff( 2 * N, N);
return c / (N + 1 );
}
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(countStrings(N) + " " );
}
}
|
Python3
def binomialCoeff(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range (k):
res * = (n - i)
res / / = (i + 1 )
return res
def countStrings(N):
if (N % 2 = = 1 ):
return 0
else :
N / / = 2
c = binomialCoeff( 2 * N, N)
return c / / (N + 1 )
if __name__ = = '__main__' :
N = 6
print (countStrings(N))
|
C#
using System;
class GFG{
static long binomialCoeff( int n, int k)
{
long res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static long countStrings( int N)
{
if (N % 2 == 1)
return 0;
else
{
N /= 2;
long c = binomialCoeff(2 * N, N);
return c / (N + 1);
}
}
public static void Main(String[] args)
{
int N = 6;
Console.Write(countStrings(N) + " " );
}
}
|
Javascript
<script>
function binomialCoeff(n , k) {
var res = 1;
if (k > n - k)
k = n - k;
for ( var i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
function countStrings(N) {
if (N % 2 == 1)
return 0;
else
{
N /= 2;
var c = binomialCoeff(2 * N, N);
return c / (N + 1);
}
}
var N = 6;
document.write(countStrings(N) + " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Aug, 2021
Like Article
Save Article