Company:Adobe
Question:
Output:
Constraints:
Example:
Question:
Given an integer K,return the kth row of pascal triangle.
Pascal's triangle is a triangular array of the binomial coefficients formed by summing up the elements of previous row.
Pascal's triangle is a triangular array of the binomial coefficients formed by summing up the elements of previous row.
Example of pascal triangle:
1
1 1
1 2 1
1 3 3 1
for K=3, return 3rd row i.e 1 2 1
1
1 1
1 2 1
1 3 3 1
for K=3, return 3rd row i.e 1 2 1
Input:
The first line contains an integer T,depicting total number of test cases.
Then following T lines contains an integer N depicting the row of triangle to be printed.
Then following T lines contains an integer N depicting the row of triangle to be printed.
Output:
Print the Nth row of triangle in a separate line.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 25
1 ≤ N ≤ 25
Example:
Input
1
4
Output
1 3 3 1
1
4
Output
1 3 3 1
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n= sc.nextInt();
for(int r=0;r<n;r++){
fact(n-1,r);
}
System.out.println();
}
}
public static void fact(int n , int r){
BigInteger f1= new BigInteger("1");
BigInteger f2= new BigInteger("1");
for(int i=n-r+1;i<n+1;i++){
f1=f1.multiply(BigInteger.valueOf(i));
}
for(int j=r;j>0;j--){f2=f2.multiply(BigInteger.valueOf(j));}
System.out.print(f1.divide(f2)+" ");
}
}
EXECUTION TIME: 0.16s
NOTE: If you observe, if I add the numbers with their place value, it results in (n-1)th power of 11, where n is the row.
Ex: If I take the 4th row, its ans is 1331 which is 11^3
Comments
Post a Comment