Find Kth row of Pascal's Triangle
You are given a non-negative integer 'K'. Your task is to find out the Kth row of Pascal’s Triangle.
In Mathematics, Pascal's triangle is a triangular array where each entry of a line is a value of a binomial coefficient. An example of Pascal’s triangle is given below.
Example :-
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.
Input Format
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”.
Output Format:
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.
Note
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= K <= 50
Where ‘T’ is the number of test cases, ‘K’ is the input row number.
Time limit: 1 sec.
CodingNinjas
author
2y
Naive Approach
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...read more
CodingNinjas
author
2y
Dynamic Programming
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 ...read more
CodingNinjas
author
2y
Dynamic Programming - Space optimized
- 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 res...read more
CodingNinjas
author
2y
Efficient Approach
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...read more
Add answer anonymously...
Top MAQ Software Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in MAQ Software Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app