Given three numbers N, X, and Y, find the count of unique binary strings of length N having at least X0s and Y1s.
Input: N=5, X=1, Y=2 Output: 25
Input: N=3, X=1, Y=1 Output: 6 Explanation: There are 3 binary strings of length 3 with at least 1 0s and 1 1s, such as: 001, 010, 100, 011, 101, 110
Naive approach: Generate all binary strings of length N and then count the number of strings with at least X0s and Y1s. Time Complexity: O(2^N) Auxiliary Space: O(1)
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 need to find the number of unique combinations for this, which can be obtained as (N)C(X) or (N)C(Y). Now for all unique binary strings, we need to find the nCi for values of i in the range [X, N-Y] and add it to a variable. The value of this sum after all iterations will be the required count.
Efficient Approach: The above approach can further be optimized with the help of the Pascal triangle to calculate nCr.Follow the steps below to solve the problem:
Please Login to comment...