Binary with no consecutive 1 with length n
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int count(int n)
{
int a[] = new int [n];
int b[] = new int [n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++)
{
a[i] = (a[i-1] + b[i-1])%1000000007;
b[i] = a[i-1];
}
return (a[n-1] + b[n-1])%1000000007;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
System.out.println(count(n));
}
}
}
import java.lang.*;
import java.io.*;
class GFG
{
static int count(int n)
{
int a[] = new int [n];
int b[] = new int [n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++)
{
a[i] = (a[i-1] + b[i-1])%1000000007;
b[i] = a[i-1];
}
return (a[n-1] + b[n-1])%1000000007;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
System.out.println(count(n));
}
}
}
0 Comments:
Post a Comment