We are given an integer N. We need to count the total number of such binary strings of length N such that the number of ‘0’s in the first string of length N/2 is double of the number of ‘0’s in the second string of length N/2.

Note: N is always an even positive integer.

**Examples:**

Input : N = 4

Output : 2

Explanation: 0010 and 0001 are two binary string such that the number of zero in the first half is double the number of zero in second half.

Input : N = 6

Output : 9

**Naive Approach:**We can generate all the binary string of length N and then one by one can check that whether selected string follows the given scenario. But the time complexity for this approach is exponential and is O(N*2^{N}).

**Efficient Approach:** This approach is based on some mathematical analysis of the problem.

Pre-requisite for this approach: Factorial and Combinatoric with modulo function.

Note: Let n = N/2.

If we perform some analysis step by step:

- The number of strings which contain two ‘0’ in first half string and one ‘0’ in second half string, is equal to
.^{n}C_{2}*^{n}C_{1} - The number of strings which contain four ‘0’ in first half string and two ‘0’ in second half string, is equal to
.^{n}C_{4}*^{n}C_{2} - The number of strings which contain six ‘0’ in first half string and three ‘0’ in second half string, is equal to
.^{n}C_{6}*^{n}C_{3}

We repeat above procedure till the number of ‘0’ in first half become n if n is even or (n-1) if n is odd.

So, our final answer is **Σ ( ^{n}C_{(2*i)} * ^{n}C_{i})**, for 2 <= 2*i <= n.

Now, we can compute the required number of strings just by using Permutation and Combination.

**Algorithm :**

int n = N/2; for(int i=2;i<=n;i=i+2) ans += ( nCr(n, i) * nCr(n, i/2);

**Note :** You can use Dynamic Programming technique to pre-compute CoeFunc(N, i) i.e. ^{n}C_{i}.

Time complexity is O(N) if we pre-compute CoeFunc(N, i) in O(N*N).

## C++

`// CPP for finding number of binary strings ` `// number of '0' in first half is double the ` `// number of '0' in second half of string ` `#include <bits/stdc++.h> ` ` ` `// pre define some constant ` `#define mod 1000000007 ` `#define max 1001 ` `using` `namespace` `std; ` ` ` `// global values for pre computation ` `long` `long` `int` `nCr[1003][1003]; ` ` ` `void` `preComputeCoeff() ` `{ ` ` ` `for` `(` `int` `i = 0; i < max; i++) { ` ` ` `for` `(` `int` `j = 0; j <= i; j++) { ` ` ` `if` `(j == 0 || j == i) ` ` ` `nCr[i][j] = 1; ` ` ` `else` ` ` `nCr[i][j] = (nCr[i - 1][j - 1] + ` ` ` `nCr[i - 1][j]) % mod; ` ` ` `} ` ` ` `} ` `} ` ` ` `// function to print number of required string ` `long` `long` `int` `computeStringCount(` `int` `N) ` `{ ` ` ` `int` `n = N / 2; ` ` ` `long` `long` `int` `ans = 0; ` ` ` ` ` `// calculate answer using proposed algorithm ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `ans = (ans + ((nCr[n][i] * nCr[n][i / 2]) ` ` ` `% mod)) % mod; ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `preComputeCoeff(); ` ` ` `int` `N = 3; ` ` ` `cout << computeStringCount(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for finding number of binary strings ` `// number of '0' in first half is double the ` `// number of '0' in second half of string ` `class` `GFG { ` ` ` ` ` `// pre define some constant ` ` ` `static` `final` `long` `mod = ` `1000000007` `; ` ` ` `static` `final` `long` `max = ` `1001` `; ` ` ` ` ` `// global values for pre computation ` ` ` `static` `long` `nCr[][] = ` `new` `long` `[` `1003` `][` `1003` `]; ` ` ` ` ` `static` `void` `preComputeCoeff() ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < max; i++) { ` ` ` `for` `(` `int` `j = ` `0` `; j <= i; j++) { ` ` ` `if` `(j == ` `0` `|| j == i) ` ` ` `nCr[i][j] = ` `1` `; ` ` ` `else` ` ` `nCr[i][j] = (nCr[i - ` `1` `][j - ` `1` `] + ` ` ` `nCr[i - ` `1` `][j]) % mod; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// function to print number of required string ` ` ` `static` `long` `computeStringCount(` `int` `N) ` ` ` `{ ` ` ` `int` `n = N / ` `2` `; ` ` ` `long` `ans = ` `0` `; ` ` ` ` ` `// calculate answer using proposed algorithm ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i += ` `2` `) ` ` ` `ans = (ans + ((nCr[n][i] * nCr[n][i / ` `2` `]) ` ` ` `% mod)) % mod; ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `preComputeCoeff(); ` ` ` `int` `N = ` `3` `; ` ` ` `System.out.println( computeStringCount(N) ); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 for finding number of binary strings ` `# number of '0' in first half is double the ` `# number of '0' in second half of string ` ` ` `# pre define some constant ` `mod ` `=` `1000000007` `Max` `=` `1001` ` ` `# global values for pre computation ` `nCr ` `=` `[[` `0` `for` `_ ` `in` `range` `(` `1003` `)] ` ` ` `for` `i ` `in` `range` `(` `1003` `)] ` ` ` `def` `preComputeCoeff(): ` ` ` ` ` `for` `i ` `in` `range` `(` `Max` `): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `): ` ` ` `if` `(j ` `=` `=` `0` `or` `j ` `=` `=` `i): ` ` ` `nCr[i][j] ` `=` `1` ` ` `else` `: ` ` ` `nCr[i][j] ` `=` `(nCr[i ` `-` `1` `][j ` `-` `1` `] ` `+` ` ` `nCr[i ` `-` `1` `][j]) ` `%` `mod ` ` ` `# function to print number of required string ` `def` `computeStringCount(N): ` ` ` `n ` `=` `N ` `/` `/` `2` ` ` `ans ` `=` `0` ` ` ` ` `# calculate answer using proposed algorithm ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `, ` `2` `): ` ` ` `ans ` `=` `(ans ` `+` `((nCr[n][i] ` `*` ` ` `nCr[n][i ` `/` `/` `2` `]) ` `%` `mod)) ` `%` `mod ` ` ` `return` `ans ` ` ` `# Driver code ` `preComputeCoeff() ` `N ` `=` `3` `print` `(computeStringCount(N)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program for finding number of binary ` `// strings number of '0' in first half is ` `// double the number of '0' in second half ` `// of string ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// pre define some constant ` ` ` `static` `long` `mod = 1000000007; ` ` ` `static` `long` `max = 1001; ` ` ` ` ` `// global values for pre computation ` ` ` `static` `long` `[,]nCr = ` `new` `long` `[1003,1003]; ` ` ` ` ` `static` `void` `preComputeCoeff() ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < max; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j <= i; j++) ` ` ` `{ ` ` ` `if` `(j == 0 || j == i) ` ` ` `nCr[i,j] = 1; ` ` ` `else` ` ` `nCr[i,j] = (nCr[i - 1,j - 1] ` ` ` `+ nCr[i - 1,j]) % mod; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// function to print number of required ` ` ` `// string ` ` ` `static` `long` `computeStringCount(` `int` `N) ` ` ` `{ ` ` ` `int` `n = N / 2; ` ` ` `long` `ans = 0; ` ` ` ` ` `// calculate answer using proposed ` ` ` `// algorithm ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `ans = (ans + ((nCr[n,i] ` ` ` `* nCr[n,i / 2]) % mod)) % mod; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `preComputeCoeff(); ` ` ` `int` `N = 3; ` ` ` `Console.Write( computeStringCount(N) ); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

**Output:**

0

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 number of rotated strings which have more number of vowels in the first half than second half
- 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
- Count of binary strings of length N having equal count of 0's and 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of Binary Strings possible as per given conditions
- Count of binary strings of given length consisting of at least one 1
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Count number of binary strings without consecutive 1's
- Count number of binary strings of length N having only 0's and 1's
- Count number of binary strings without consecutive 1’s : Set 2
- Count binary strings with k times appearing adjacent two set bits
- Count of distinct XORs formed by rearranging two Binary strings
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Maximum number of strings that can be formed with given zeros and ones
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum difference of zeros and ones in binary string
- Find all even length binary sequences with same sum of first and second half bits
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)

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.