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++
// 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; } |
C
// C Program to implement the above approach #include <stdio.h> #include <math.h> // 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; printf ( "%lu" , countOfString(N)); return 0; } // This code is contributed by Sania Kumari Gupta |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Javascript program to implement // the above approach // Function to calculate // and return the value of // Binomial Coefficient C(n, k) function binomialCoeff(n, k) { let 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 (let 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 function countOfString(N) { // Count of N-length binary Strings let Stotal = Math.pow(2, N); // Count of N- length binary Strings // having equal count of 0's and 1's let Sequal = 0; // For even length Strings if (N % 2 == 0) Sequal = binomialCoeff(N, N / 2); let S1 = (Stotal - Sequal) / 2; return S1; } let N = 3; document.write(countOfString(N)); // This code is contributed by divyeshrabadiya07. </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)