β Kth Row of Pascal's Triangle(Contest)
Kth Row of Pascal's Triangle easy Time Limit: 2 sec Memory Limit: 128000 kB
Problem Statement :
Given an index K, return the K-th row of Pascalβs triangle. You must print the K-th row modulo 109 + 7. The rows of Pascal's triangle are conventionally enumerated starting with row K=0 at the top (the 0th row)InputThe only line of input contains the input K. Constraints:- 0 β€ K β€ 3000 OutputPrint k-th row of Pascal's Triangle containing k+1 integers modulo 10^9+7.ExampleSample Input : 3 Sample Output: 1 3 3 1
link:https://my.newtonschool.co/playground/code/ynuml7qftqz8/
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static final int mod = 1000000007;
public static int add(int a, int b) {
int res = a + b;
if (res >= mod) {
res -= mod;
}
return res;
}
public static int sub(int a, int b) {
int res = a - b;
if (res < 0) {
res += mod;
}
return res;
}
public static int mul(int a, int b) {
return (int) ((long) a * b % mod);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
ArrayList<Integer> row = new ArrayList<>(k + 1);
row.add(1);
for (int i = 1; i <= k; i++) {
int n = row.size();
for (int j = n - 2; j >= 0; j--) {
row.set(j + 1, add(row.get(j), row.get(j + 1)));
}
row.add(1);
}
for (int i = 0; i <= k; i++) {
System.out.print(row.get(i) + " ");
}
}
}
```
Last updated