Given two integers X and Y, the task is to count the number of binary strings which have X 1’s and Y 0’s and there are no two consecutive 1’s in the string.
Examples:
Input: X = 2, Y = 2 Output: 3 Explanation: 1010, 0101, 1001 – these are 3 strings that can be possible such that there are no two consecutive 1’s.
Input: X = 5, Y = 3 Output: 0 Explanation: There is no such string that can be possible.
Approach: This can be solved with the following idea:
First calculating factorial of both the given numbers and then calculating power of each one will led us to get the desired answer.
Below are the steps involved:
- Calculating rem Y – X + 1.
- Calculate the factorial of Y+1 and X by calling ncr function.
- Also, found out the power of both numbers using the binpow function.
- For the answer, multiply the factorial of X with the power of y.
- Then final, multiply answer with power of X.
- Return answer.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long binpow( long long a, long long b)
{
long long ans = 1;
while (b) {
if (b % 2 == 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b = b / 2;
}
return ans;
}
long long ncr( long long a, long long b)
{
long long facta = 1;
long long factb = 1;
long long factab = 1;
for ( int i = a; i >= 1; i--) {
facta = (facta * i);
}
for ( int j = b; j >= 1; j--) {
factb = (factb * j);
}
long long x = (a - b);
for ( int i = x; i >= 1; i--) {
factab = (factab * i);
}
long long invab = binpow(factab, mod - 2);
long long invb = binpow(factb, mod - 2);
long long answer = (facta * invb) % mod;
answer = (answer * invab) % mod;
return answer;
}
int CountString( int x, int y)
{
long long rem = y - x + 1;
if (rem < 0) {
return 0;
}
long long answer = ncr(y + 1, x);
return answer;
}
int main()
{
int x = 2;
int y = 2;
cout << CountString(x, y);
return 0;
}
|
Java
import java.util.*;
public class CountString {
static long mod = 1000000007 ;
static long binpow( long a, long b) {
long ans = 1 ;
while (b > 0 ) {
if (b % 2 == 1 ) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b /= 2 ;
}
return ans;
}
static long ncr( long a, long b) {
long facta = 1 ;
long factb = 1 ;
long factab = 1 ;
for ( long i = a; i >= 1 ; i--) {
facta = (facta * i) % mod;
}
for ( long j = b; j >= 1 ; j--) {
factb = (factb * j) % mod;
}
long x = a - b;
for ( long i = x; i >= 1 ; i--) {
factab = (factab * i) % mod;
}
long invab = binpow(factab, mod - 2 );
long invb = binpow(factb, mod - 2 );
long answer = (facta * invb) % mod;
answer = (answer * invab) % mod;
return answer;
}
static int countString( int x, int y) {
long rem = y - x + 1 ;
if (rem < 0 ) {
return 0 ;
}
long answer = ncr(y + 1 , x);
return ( int ) answer;
}
public static void main(String[] args) {
int x = 2 ;
int y = 2 ;
System.out.println(countString(x, y));
}
}
|
Python3
mod = 10 * * 9 + 7
def binpow(a, b):
ans = 1
while b > 0 :
if b % 2 = = 1 :
ans = (ans * a) % mod
a = (a * a) % mod
b = b / / 2
return ans
def ncr(a, b):
facta = 1
factb = 1
factab = 1
for i in range (a, 0 , - 1 ):
facta = (facta * i)
for j in range (b, 0 , - 1 ):
factb = (factb * j)
x = a - b
for i in range (x, 0 , - 1 ):
factab = (factab * i)
invab = binpow(factab, mod - 2 )
invb = binpow(factb, mod - 2 )
answer = (facta * invb) % mod
answer = (answer * invab) % mod
return answer
def CountString(x, y):
rem = y - x + 1
if rem < 0 :
return 0
answer = ncr(y + 1 , x)
return answer
x = 2
y = 2
print (CountString(x, y))
|
C#
using System;
public class GFG {
static int mod = 1000000007;
static long binpow( long a, long b)
{
long ans = 1;
while (b > 0) {
if (b % 2 == 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b = b / 2;
}
return ans;
}
static long ncr( long a, long b)
{
long facta = 1;
long factb = 1;
long factab = 1;
for ( int i = ( int )a; i >= 1; i--) {
facta = (facta * i);
}
for ( int j = ( int )b; j >= 1; j--) {
factb = (factb * j);
}
long x = (a - b);
for ( int i = ( int )x; i >= 1; i--) {
factab = (factab * i);
}
long invab = binpow(factab, mod - 2);
long invb = binpow(factb, mod - 2);
long answer = (facta * invb) % mod;
answer = (answer * invab) % mod;
return answer;
}
static int CountString( int x, int y)
{
long rem = y - x + 1;
if (rem < 0) {
return 0;
}
long answer = ncr(y + 1, x);
return ( int )answer;
}
static void Main()
{
int x = 2;
int y = 2;
Console.WriteLine(CountString(x, y));
}
}
|
Javascript
const mod = BigInt(10**9 + 7);
function binpow(a, b) {
let ans = BigInt(1);
while (b > 0n) {
if (b % 2n === 1n) {
ans = (ans * BigInt(a)) % mod;
}
a = (BigInt(a) * BigInt(a)) % mod;
b = b / 2n;
}
return ans;
}
function ncr(a, b) {
let facta = BigInt(1);
let factb = BigInt(1);
let factab = BigInt(1);
for (let i = BigInt(a); i >= 1n; i--) {
facta = (facta * i) % mod;
}
for (let j = BigInt(b); j >= 1n; j--) {
factb = (factb * j) % mod;
}
let x = BigInt(a - b);
for (let i = x; i >= 1n; i--) {
factab = (factab * i) % mod;
}
let invab = binpow(factab, mod - 2n);
let invb = binpow(factb, mod - 2n);
let answer = (facta * invb) % mod;
answer = (answer * invab) % mod;
return answer;
}
function countString(x, y) {
let rem = BigInt(y - x + 1);
if (rem < 0n) {
return 0;
}
let answer = ncr(BigInt(y + 1), BigInt(x));
return Number(answer);
}
const x = 2;
const y = 2;
console.log(countString(x, y));
|
Time Complexity: O(n*log n) Auxiliary Space: O(1)
|