Count of Binary Strings of length N such that frequency of 1’s exceeds frequency of 0’s
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”}.
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)
Auxiliary 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
- For every string in S1, there exist a string in S0.
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).
- If N is odd then Sequal = 0.
- If N is even then Sequal =C(N, N/2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unsigned long int binomialCoeff(unsigned long int n,
unsigned long int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int countOfString( int N)
{
unsigned long int Stotal = pow (2, N);
unsigned long int Sequal = 0;
if (N % 2 == 0)
Sequal = binomialCoeff(N, N / 2);
unsigned long int S1 = (Stotal - Sequal) / 2;
return S1;
}
int main()
{
int N = 3;
cout << countOfString(N);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
unsigned long int binomialCoeff(unsigned long int n,
unsigned long int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int countOfString( int N)
{
unsigned long int Stotal = pow (2, N);
unsigned long int Sequal = 0;
if (N % 2 == 0)
Sequal = binomialCoeff(N, N / 2);
unsigned long int S1 = (Stotal - Sequal) / 2;
return S1;
}
int main()
{
int N = 3;
printf ( "%lu" , countOfString(N));
return 0;
}
|
Java
import java.util.*;
class GFG{
static int binomialCoeff( int n, int k)
{
int res = 1 ;
if (k > n - k)
k = n - k;
for ( int i = 0 ; i < k; ++i)
{
res *= (n - i);
res /= (i + 1 );
}
return res;
}
static int countOfString( int N)
{
int Stotal = ( int ) Math.pow( 2 , N);
int Sequal = 0 ;
if (N % 2 == 0 )
Sequal = binomialCoeff(N, N / 2 );
int S1 = (Stotal - Sequal) / 2 ;
return S1;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(countOfString(N));
}
}
|
Python3
def binomialCoeff(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range (k):
res * = (n - i)
res / / = (i + 1 )
return res
def countOfString(N):
Stotal = pow ( 2 , N)
Sequal = 0
if (N % 2 = = 0 ):
Sequal = binomialCoeff(N, N / / 2 )
S1 = (Stotal - Sequal) / / 2
return S1
N = 3
print (countOfString(N))
|
C#
using System;
class GFG{
static int binomialCoeff( int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static int countOfString( int N)
{
int Stotal = ( int ) Math.Pow(2, N);
int Sequal = 0;
if (N % 2 == 0)
Sequal = binomialCoeff(N, N / 2);
int S1 = (Stotal - Sequal) / 2;
return S1;
}
public static void Main(String[] args)
{
int N = 3;
Console.Write(countOfString(N));
}
}
|
Javascript
<script>
function binomialCoeff(n, k)
{
let res = 1;
if (k > n - k)
k = n - k;
for (let i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
function countOfString(N)
{
let Stotal = Math.pow(2, N);
let Sequal = 0;
if (N % 2 == 0)
Sequal = binomialCoeff(N, N / 2);
let S1 = (Stotal - Sequal) / 2;
return S1;
}
let N = 3;
document.write(countOfString(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...