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 = 2_

Output:_ 1_

_Explanation: __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 = 3_

Output:_ 4_

_Explanation: _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”}.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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*2N) _

Auxilairy Space:_ O(1)_

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

_STotal __= Total Number of binary strings of length N = 2N _

_Sequal __= Number of binary string of length N having same frequency of 0’s and 1’s. _

S1_ = Number of binary strings of length N having frequency of 1’s greater than 0’s. _

S0_ = Number of binary strings of length N having frequency of 0’s greater than 1’s. _

Stotal = Sequal + S1 + S0

  1. For every string in S1, there exist a string in S0.
  2. Suppose “1110” is the string in S1 then its corresponding string in S0  will be “0001“. Similarly, for every string in S1 there exist a string in S0. Hence, S1 = S0 ( for every N).
  3. If N is odd then** Sequal = 0**.
  4. If N is even then Sequal =C(N, N/2).

Below is the implementation of the above approach:

  • C++
  • Python3

// 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;

}

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.

#bit magic #combinatorial #mathematical #strings #binary-string #frequency-counting #setbitcount

Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
5.95 GEEK