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 greater than that of 0’s is “11”.

Input:N = 3Output:4Explanation:Count of binary strings of length 3 is 8 i.e. {“000”, “001”, “010”, “011”, “100”, “101”, “110”, “111”}.

Among them, the strings having frequency of 1’s greater than 0’s are {“011”, “101”, “110”, “111”}.

**Naive Approach:** The simplest approach to solve this problem is to generate all binary strings of length N, and iterate over each string to find the frequency of 1’s and 0’s. If the frequency of 1’s is greater than that of 0’s, increment the counter. Finally, print the count.

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

**Efficient Approach:** To observe the above approach, following observations need to made:

S= Total Number of binary strings of length N = 2_{Total }^{N}

S= Number of binary string of length N having same frequency of 0’s and 1’s._{equal}S= Number of binary strings of length N having frequency of 1’s greater than 0’s._{1}S= Number of binary strings of length N having frequency of 0’s greater than 1’s._{0}S_{total }= S_{equal }+ S_{1 }+ S_{0}

- For every string in
**S**, there exist a string in_{1}**S**_{0.}

Suppose “**1110**” is the string in**S**then its corresponding string in_{1}**S**will be “_{0}**0001**“. Similarly, for every string in**S**_{1}_{}there exist a string in**S**. Hence,_{0}**S**_{1}= S_{0}_{}( for every**N**). - If
**N**is**odd**then**S**._{equal }= 0 - If
**N**is**even**then**S**._{equal}=C(N, N/2)

Below is the implementation of the above approach:

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to calculate ` `// and return the value of ` `// Binomial Coefficient C(n, k) ` `unsigned ` `long` `int` `binomialCoeff(unsigned ` `long` `int` `n, `
` ` `unsigned ` `long` `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 ` `// binary strings of length N such ` `// that frequency of 1's exceed that of 0's ` `unsigned ` `long` `int` `countOfString(` `int` `N) `
`{ ` ` ` `// Count of N-length binary strings `
` ` `unsigned ` `long` `int` `Stotal = ` `pow` `(2, N); `
` ` ` ` `// Count of N- length binary strings `
` ` `// having equal count of 0's and 1's `
` ` `unsigned ` `long` `int` `Sequal = 0; `
` ` ` ` `// For even length strings `
` ` `if` `(N % 2 == 0) `
` ` `Sequal = binomialCoeff(N, N / 2); `
` ` ` ` `unsigned ` `long` `int` `S1 = (Stotal - Sequal) / 2; `
` ` `return` `S1; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `N = 3; `
` ` `cout << countOfString(N); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to implement ` `// the above approach ` `import` `java.util.*; `
` ` `class` `GFG{ `
` ` `// Function to calculate ` `// and return the value of ` `// Binomial Coefficient C(n, k) ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) `
`{ ` ` ` `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 ` `// binary Strings of length N such ` `// that frequency of 1's exceed that of 0's ` `static` `int` `countOfString(` `int` `N) `
`{ ` ` ` ` ` `// Count of N-length binary Strings `
` ` `int` `Stotal = (` `int` `) Math.pow(` `2` `, N); `
` ` ` ` `// Count of N- length binary Strings `
` ` `// having equal count of 0's and 1's `
` ` `int` `Sequal = ` `0` `; `
` ` ` ` `// For even length Strings `
` ` `if` `(N % ` `2` `== ` `0` `) `
` ` `Sequal = binomialCoeff(N, N / ` `2` `); `
` ` ` ` `int` `S1 = (Stotal - Sequal) / ` `2` `; `
` ` `return` `S1; `
`} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) `
`{ ` ` ` `int` `N = ` `3` `; `
` ` `System.out.print(countOfString(N)); `
`} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to calculate ` `# and return 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 ` `# binary strings of length N such ` `# that frequency of 1's exceed that of 0's ` `def` `countOfString(N): `
` ` ` ` `# Count of N-length binary strings `
` ` `Stotal ` `=` `pow` `(` `2` `, N) `
` ` ` ` `# Count of N- length binary strings `
` ` `# having equal count of 0's and 1's `
` ` `Sequal ` `=` `0`
` ` ` ` `# For even length strings `
` ` `if` `(N ` `%` `2` `=` `=` `0` `): `
` ` `Sequal ` `=` `binomialCoeff(N, N ` `/` `/` `2` `) `
` ` ` ` `S1 ` `=` `(Stotal ` `-` `Sequal) ` `/` `/` `2`
` ` ` ` `return` `S1 `
` ` `# Driver Code ` `N ` `=` `3`
` ` `print` `(countOfString(N)) `
` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

`// C# program to implement ` `// the above approach ` `using` `System; `
`class` `GFG{ `
` ` `// Function to calculate ` `// and return the value of ` `// Binomial Coefficient C(n, k) ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) `
`{ ` ` ` `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 ` `// binary Strings of length N such ` `// that frequency of 1's exceed that of 0's ` `static` `int` `countOfString(` `int` `N) `
`{ ` ` ` ` ` `// Count of N-length binary Strings `
` ` `int` `Stotal = (` `int` `) Math.Pow(2, N); `
` ` ` ` `// Count of N- length binary Strings `
` ` `// having equal count of 0's and 1's `
` ` `int` `Sequal = 0; `
` ` ` ` `// For even length Strings `
` ` `if` `(N % 2 == 0) `
` ` `Sequal = binomialCoeff(N, N / 2); `
` ` ` ` `int` `S1 = (Stotal - Sequal) / 2; `
` ` `return` `S1; `
`} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) `
`{ ` ` ` `int` `N = 3; `
` ` `Console.Write(countOfString(N)); `
`} ` `} ` ` ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

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:

- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- 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
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Number of binary strings such that there is no substring of length ≥ 3
- Count of same length Strings that exists lexicographically in between two given Strings
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count number of binary strings of length N having only 0's and 1's
- Count of binary strings of given length consisting of at least one 1
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Print all strings of maximum length from an array of strings
- Distinct strings such that they contains given strings as sub-sequences
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Count pairs in an array such that frequency of one is at least value of other

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.