Generating Pascal's Triangle
Given an integer N
, generate a 2-D array list representing Pascal's Triangle up to the N-th row.
An element in Pascal's Triangle is the sum of the two elements directly above it in the previous row. The triangle also represents binomial coefficients.
Input:
The first line contains an integerT
, the number of test cases.
Each test case consists of a single integerN
, representing the number of rows in the Pascal's Triangle to be returned.
Output:
For each test case, return a 2-D array list containing Pascal's Triangle up to row N
.
Example:
Input:
T = 1 N = 4
Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
Explanation:
For N = 4
, the Pascal's Triangle is:
[
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1]
]
Each element is obtained by summing the two directly above it.
Constraints:
1 <= T <= 40
1 <= N <= 50
- Time Limit: 1 sec
Note:
The implementation function should return the result, not print it.

AnswerBot
4mo
Generate Pascal's Triangle up to N-th row using 2-D array list.
Create a 2-D array list to store the Pascal's Triangle elements.
Iterate through each row and calculate the elements based on the sum of t...read more
Help your peers!
Add answer anonymously...
>
INCA INFOTECH TECHNOLOGIES Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

