Count of binary string of length N with X 0s and Y 1s
Given positive integers N, X and Y. The task is to find the count of unique binary strings of length N having X 0s and Y 1s.
Examples:
Input: N=5, X=3, Y=2
Output: 10
Explanation: There are 10 binary strings of length 5 with 3 0s and 2 1s, such as:
00011, 00101, 01001, 10001, 00110, 01010, 10010, 01100, 10100, 11000.
Input: N=3, X=1, Y=2
Output: 3
Explanation: There are 3 binary strings of length 3 with 1 0s and 2 1s, such as: 011, 101, 110
Naive approach: Generate all binary strings of length N and then count the number of strings with X 0s and Y 1s.
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Better Approach: This problem can also be solved using Combinatorics. If the length is N, and given is X 0s, then there will be Y = (N – X) 1s. So we can think of this as a N length string with X 0s and Y 1s. We need to find the number of unique combinations for this, which can be obtained as _{X}^{N}\textrm{C} or _{Y}^{N}\textrm{C}. This can be done using Pascal triangle to calculate the value of combination.
Time Complexity: O(N)
Space Complexity: O(N2)
Note: This approach is the best one if there are multiple queries for X and Y. Then also it will have the same time and space complexity.
Space Optimised Approach: The space consumption in the above approach can be optimised if we take help of the formula _{X}^{N}\textrm{C} = N!/(X!*(N-X)!) and calculate the value by using the factorials.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int fact( int f)
{
f++;
long long int ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
long long int countWays( int N, int X, int Y)
{
return (fact(N) / (fact(X) * fact(Y)));
}
int main()
{
int N = 5, X = 3, Y = 2;
cout << countWays(N, X, Y) << endl;
return 0;
}
|
Java
public class GFG{
static int fact( int f)
{
f++;
int ans = 1 ;
while (--f > 0 )
ans = ans * f;
return ans;
}
static int countWays( int N, int X, int Y)
{
return (fact(N) / (fact(X) * fact(Y)));
}
public static void main(String args[])
{
int N = 5 , X = 3 , Y = 2 ;
System.out.println(countWays(N, X, Y));
}
}
|
Python3
def fact(f):
ans = 1 ;
while (f):
ans = ans * f;
f - = 1
return ans;
def countWays(N, X, Y):
return fact(N) / / (fact(X) * fact(Y));
N = 5
X = 3
Y = 2
print (countWays(N, X, Y))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int fact( int f)
{
f++;
int ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
static int countWays( int N, int X, int Y)
{
return (fact(N) / (fact(X) * fact(Y)));
}
public static void Main()
{
int N = 5, X = 3, Y = 2;
Console.Write(countWays(N, X, Y));
}
}
|
Javascript
<script>
function fact(f) {
f++;
let ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
function countWays(N, X, Y) {
return Math.floor(fact(N) / (fact(X) * fact(Y)));
}
let N = 5, X = 3, Y = 2;
document.write(countWays(N, X, Y))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Note: In case of multiple(Q) queries this approach will have time complexity O(Q*N).
Last Updated :
10 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...