Given A Pattern As Below and An Integer N Your Task Is To Decode It and Print NTH Row of It
Given A Pattern As Below and An Integer N Your Task Is To Decode It and Print NTH Row of It
Input:
The first line of input is the number of test cases . Then T test cases
follow . The first line of each test case is an integer N.
Output:
For each test case print the required nth row of the pattern.
Constraints:
1<=T<=20
1<=N<=20
Example:
Input:
2
2
3
Output:
11
21
Solution-1:
class GFG {
int times= 1;
if(actual != repeat){
times= 1;
repeat= actual;
}else{
times+= 1;
return result.toString();
while(loop > 0)
num = lookandsay(num);
}
System.out.println(num);
loop --;
Solution-2:
public class test{
class GFG {
public static void main (String[] args) {
//code
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
if(n==1)
{
System.out.println(1);
continue;
}
if(n==2)
{
System.out.println(11);
continue;
}
String str="11";
for(int i=3;i<=n;i++)
{
str+="$";
String tmp="";
int cnt=1;
char a[]=str.toCharArray();
for(int i1=1;i1<a.length;i1++)
{
if(a[i1]!=a[i1-1])
{
tmp+=cnt;
tmp+=a[i1-1];
cnt=1;
}
else
cnt++;
}
str=tmp;
}
System.out.println(str);
}
}
}
Solution-5:
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
for(int i = 0; i<tc; i++) {
int nthRow = sc.nextInt();
System.out.println(pattern(nthRow));
}
}
public static String pattern(int size) {
String[] str = new String[size+1];
str[0] = "1";
for(int i=1; i<=size; i++) {
String subLiteral = str[i-1];
StringBuilder out = new StringBuilder("");
for(int j = 0; j<subLiteral.length(); j++) {
int subCount = 1;
while(j < subLiteral.length()-1 && subLiteral.charAt(j) ==
subLiteral.charAt(j+1)) {
subCount++;
j++;
}
out.append(Integer.toString(subCount)).append(Character.toString(su
bLiteral.charAt(j)));
}
str[i] = out.toString();
}
return str[size-1];
}
}