New update is available. Click here to update.

Last Updated: 9 Jan, 2021

Easy

```
INPUT : K = 2
OUTPUT: 1 1
In the above example, K = 2, Hence the 2nd row from the top of pascalβs triangle, as shown in the above example is 1 1.
INPUT : K = 4
OUTPUT : 1 4 6 4 1
In the above example, K = 4, Hence the 4th row from the top of pascalβs triangle, as shown in the above example is 1 3 3 1.
```

```
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first and the only line of each test case contains a single integer βKβ.
```

```
For every test case, print a single line containing 'R' space-separated integers showing the Kth row of pascalβs triangle, where 'R' is the number of elements in a particular row.
The output of each test case will be printed in a separate line.
```

```
You donβt have to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 50
1 <= K <= 50
Where βTβ is the number of test cases, βKβ is the input row number.
Time limit: 1 sec.
```

Approaches

In this approach, we find the row elements of the previous row using recursion, and based on the values of previous row elements, we will evaluate the current row elements.

- We are given a function
**kthRow()**which takes an integer βKβ as the only parameter and returns an integer vector. This is the definition of our recursive function too. - As the base condition of our recursive function, we will check if βKβ is equal to 1 or not. If βKβ is 1, we will return the vector containing 1.
- Now we will call our recursive function kthRow() with K - 1 as our new parameter to store the previous row elements.
- Now by adding the previous row elements, we will find the current row elements and then return the current row.

In this approach, we will build our solution in the bottom-up manner storing the results in a 2D array βDPβ.

- We are given a function
**kthRow()**which takes an integer βKβ as the only parameter and returns an integer vector. - Initialize a vector βRESULTβ to store the final result.
- Initialize a 2D integer array βDPβ with both rows and columns equal to βKβ.
- Now in a nested loop, we will build our array βDPβ using the bottom-up approach, forming the current element by adding values just above it.
- Finally, add the final row elements of βDPβ in the array βRESULTβ and return it as our final answer.

- We are given a function
**kthRow()**which takes an integer βKβ as the only parameter and returns an integer vector. - Initialize a vector βRESULTβ to store the final result.
- Initialize two arrays of size K, as βPREVROWβ and βCURRROWβ to store previous row and current row of the triangle.
- Now in a nested loop, we will build our array βCURRROWβ using the bottom-up approach, forming the current element by adding values of βPREVROWβ.
- Finally, add the final row elements of βCURRROWβ in the array βRESULTβ and return it as our final answer.

In this approach, we will try to observe the pattern and make the sequence for the current row elements. We can clearly see that the Kth row of pascalβs triangle has the sequence : C(K, 0), C(K, 1), ..., C(K, K - 1), C(K, K), where C(N, R) is the binomial coefficient or Combination function. As C(K, 0) = 1, we can evaluate other values of the sequence by the formula, C(N, R) = (C(N, R - 1) * (N - R )) / R .

- We are given a function
**kthRow()**which takes an integer βKβ as the only parameter and returns an integer vector. - We will store the value of the first coefficient, i.e C(K, 0) or 1 in a variable βPREVβ. This variable will be used to store all the previous coefficient values
- Then in the loop, until we reach K, we will evaluate the value of each element of the Kth row and store it in the vector βROWVECTORβ, simultaneously storing the current value in βPREVβ for further use.
- Finally, we will return βROWVECTORβ as our answer.