Count of binary strings of length N having equal count of 0’s and 1’s
Last Updated :
05 May, 2021
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 possible binary strings of length 2 are “00”, “01”, “10” and “11”.
Among them, “10” and “01” have the same frequency of 0s and 1s.
Hence, the answer is 2.
Input: 4
Output: 6
Explanation:
Strings “0011”, “0101”, “0110”, “1100”, “1010” and “1001” have same frequency of 0s and 1s.
Hence, the answer is 6.
Naive Approach:
The simplest approach is to generate all possible permutations of a string of length N having equal number of ‘0’ and ‘1’. For every permutation generated, increase the count. Print the total count of permutations generated.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach:
The above approach can be optimized by using concepts of Permutation and Combination. Follow the steps below to solve the problem:
- Since N positions need to be filled with equal number of 0‘s and 1‘s, select N/2 positions from the N positions in C(N, N/2) % mod( where mod = 109 + 7) ways to fill with only 1’s.
- Fill the remaining positions in C(N/2, N/2) % mod (i.e 1) way with only 0’s.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int nCrModp( int n, int r)
{
if (n % 2 == 1) {
return -1;
}
int C[r + 1];
memset (C, 0, sizeof (C));
C[0] = 1;
for ( int i = 1; i <= n; i++) {
for ( int j = min(i, r); j > 0;
j--)
C[j] = (C[j] + C[j - 1])
% MOD;
}
return C[r];
}
int main()
{
int N = 6;
cout << nCrModp(N, N / 2);
return 0;
}
|
Java
import java.util.*;
class GFG{
final static int MOD = 1000000007 ;
static int nCrModp( int n, int r)
{
if (n % 2 == 1 )
{
return - 1 ;
}
int C[] = new int [r + 1 ];
Arrays.fill(C, 0 );
C[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++)
{
for ( int j = Math.min(i, r);
j > 0 ; j--)
C[j] = (C[j] + C[j - 1 ]) % MOD;
}
return C[r];
}
public static void main(String s[])
{
int N = 6 ;
System.out.println(nCrModp(N, N / 2 ));
}
}
|
Python3
MOD = 1000000007
def nCrModp (n, r):
if (n % 2 = = 1 ):
return - 1
C = [ 0 ] * (r + 1 )
C[ 0 ] = 1
for i in range ( 1 , n + 1 ):
for j in range ( min (i, r), 0 , - 1 ):
C[j] = (C[j] + C[j - 1 ]) % MOD
return C[r]
N = 6
print (nCrModp(N, N / / 2 ))
|
C#
using System;
class GFG{
static int MOD = 1000000007;
static int nCrModp( int n, int r)
{
if (n % 2 == 1)
{
return -1;
}
int [] C = new int [r + 1];
C[0] = 1;
for ( int i = 1; i <= n; i++)
{
for ( int j = Math.Min(i, r);
j > 0; j--)
C[j] = (C[j] + C[j - 1]) % MOD;
}
return C[r];
}
static void Main()
{
int N = 6;
Console.WriteLine(nCrModp(N, N / 2));
}
}
|
Javascript
<script>
var MOD = 1000000007;
function nCrModp(n , r) {
if (n % 2 == 1) {
return -1;
}
var C = Array(r + 1).fill(0);
C[0] = 1;
for (i = 1; i <= n; i++) {
for (j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % MOD;
}
return C[r];
}
var N = 6;
document.write(nCrModp(N, N / 2));
</script>
|
Time Complexity: O(N2)
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...