Given an integer **N**, the task is to find the number of binary strings possible of length **N** having same frequency of **0**s and **1**s. If such string is possible of length **N**, print **-1**. **Note:** Since the count can be very large, return the answer modulo **10 ^{9}+7**.

**Examples:**

Input:N = 2Output:2Explanation:

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:4Output:6Explanation:

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 permutatiosn 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 = 10^{9}+ 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++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MOD 1000000007 ` ` ` `// Function to calculate C(n, r) % MOD ` `// DP based approach ` `int` `nCrModp(` `int` `n, ` `int` `r) ` `{ ` ` ` `// Corner case ` ` ` `if` `(n % 2 == 1) { ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Stores the last row ` ` ` `// of Pascal's Triangle ` ` ` `int` `C[r + 1]; ` ` ` ` ` `memset` `(C, 0, ` `sizeof` `(C)); ` ` ` ` ` `// Initialize top row ` ` ` `// of pascal triangle ` ` ` `C[0] = 1; ` ` ` ` ` `// Construct Pascal's Triangle ` ` ` `// from top to bottom ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// Fill current row with the ` ` ` `// help of previous row ` ` ` `for` `(` `int` `j = min(i, r); j > 0; ` ` ` `j--) ` ` ` ` ` `// C(n, j) = C(n-1, j) ` ` ` `// + C(n-1, j-1) ` ` ` `C[j] = (C[j] + C[j - 1]) ` ` ` `% MOD; ` ` ` `} ` ` ` ` ` `return` `C[r]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `cout << nCrModp(N, N / 2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `final` `static` `int` `MOD = ` `1000000007` `; ` ` ` `// Function to calculate C(n, r) % MOD ` `// DP based approach ` `static` `int` `nCrModp(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `// Corner case ` ` ` `if` `(n % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Stores the last row ` ` ` `// of Pascal's Triangle ` ` ` `int` `C[] = ` `new` `int` `[r + ` `1` `]; ` ` ` ` ` `Arrays.fill(C, ` `0` `); ` ` ` ` ` `// Initialize top row ` ` ` `// of pascal triangle ` ` ` `C[` `0` `] = ` `1` `; ` ` ` ` ` `// Construct Pascal's Triangle ` ` ` `// from top to bottom ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Fill current row with the ` ` ` `// help of previous row ` ` ` `for` `(` `int` `j = Math.min(i, r); ` ` ` `j > ` `0` `; j--) ` ` ` ` ` `// C(n, j) = C(n-1, j) ` ` ` `// + C(n-1, j-1) ` ` ` `C[j] = (C[j] + C[j - ` `1` `]) % MOD; ` ` ` `} ` ` ` `return` `C[r]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String s[]) ` `{ ` ` ` `int` `N = ` `6` `; ` ` ` `System.out.println(nCrModp(N, N / ` `2` `)); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` `MOD ` `=` `1000000007` ` ` `# Function to calculate C(n, r) % MOD ` `# DP based approach ` `def` `nCrModp (n, r): ` ` ` ` ` `# Corner case ` ` ` `if` `(n ` `%` `2` `=` `=` `1` `): ` ` ` `return` `-` `1` ` ` ` ` `# Stores the last row ` ` ` `# of Pascal's Triangle ` ` ` `C ` `=` `[` `0` `] ` `*` `(r ` `+` `1` `) ` ` ` ` ` `# Initialize top row ` ` ` `# of pascal triangle ` ` ` `C[` `0` `] ` `=` `1` ` ` ` ` `# Construct Pascal's Triangle ` ` ` `# from top to bottom ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# Fill current row with the ` ` ` `# help of previous row ` ` ` `for` `j ` `in` `range` `(` `min` `(i, r), ` `0` `, ` `-` `1` `): ` ` ` ` ` `# C(n, j) = C(n-1, j) ` ` ` `# + C(n-1, j-1) ` ` ` `C[j] ` `=` `(C[j] ` `+` `C[j ` `-` `1` `]) ` `%` `MOD ` ` ` ` ` `return` `C[r] ` ` ` `# Driver Code ` `N ` `=` `6` ` ` `print` `(nCrModp(N, N ` `/` `/` `2` `)) ` ` ` `# This code is contributed by himanshu77 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `static` `int` `MOD = 1000000007; ` ` ` `// Function to calculate C(n, r) % MOD ` `// DP based approach ` `static` `int` `nCrModp(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `// Corner case ` ` ` `if` `(n % 2 == 1) ` ` ` `{ ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Stores the last row ` ` ` `// of Pascal's Triangle ` ` ` `int` `[] C = ` `new` `int` `[r + 1]; ` ` ` ` ` `// Initialize top row ` ` ` `// of pascal triangle ` ` ` `C[0] = 1; ` ` ` ` ` `// Construct Pascal's Triangle ` ` ` `// from top to bottom ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Fill current row with the ` ` ` `// help of previous row ` ` ` `for` `(` `int` `j = Math.Min(i, r); ` ` ` `j > 0; j--) ` ` ` ` ` `// C(n, j) = C(n-1, j) ` ` ` `// + C(n-1, j-1) ` ` ` `C[j] = (C[j] + C[j - 1]) % MOD; ` ` ` `} ` ` ` `return` `C[r]; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `N = 6; ` ` ` `Console.WriteLine(nCrModp(N, N / 2)); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

**Output:**

20

**Time Complexity: **O(N^{2}) **Space Complexity: **O(N)

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 and count of 1's ≥ count of 0's in each prefix substring
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count number of binary strings of length N having only 0's and 1's
- 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
- Count the number of subsequences of length k having equal LCM and HCF
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of Substrings having Sum equal to their Length
- Count of subarrays having sum equal to its length
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Count of same length Strings that exists lexicographically in between two given Strings
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count of strings that become equal to one of the two strings after one removal
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Longest palindrome formed by concatenating and reordering strings of equal length
- Count of binary strings of given length consisting of at least one 1
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's

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.