Pascal's Triangle

You are given an integer N. Your task is to return a 2-D ArrayList containing the pascal’s triangle till the row N.

A Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient. For example in the figure below.

For example, given integer N= 4 then you have to print.

1  
1 1 
1 2 1 
1 3 3 1

Here for the third row, you will see that the second element is the summation of the above two-row elements i.e. 2=1+1, and similarly for row three 3 = 1+2 and 3 = 1+2.
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer N denoting the row till which you have to print the pascal’s triangle.
Output format :
For each test case, return the 2-D array/list containing the pascal’s triangle till the row N.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 40
1 <= N <= 50

Time Limit: 1 sec
CodingNinjas
author
2y

Approach : This was a preety simple question so I was directly asked to code it using any of my preferred IDE.

Pascal’s triangle is a triangular array of the binomial coefficients.

//Pseudo Code

void Pas...read more

CodingNinjas
author
2y
Recursive Solution

The idea is to use recursion to get the value of the coefficients we will create a helper function CALPASCAL which will take row and entry as its input parameters and we call this fu...read more

CodingNinjas
author
2y
Using previous rows to make new rows

The idea is to use the definition of pascal’s triangle that its coefficients and are summation of adjacent elements in preceding rows. so in this way we can store t...read more

CodingNinjas
author
2y
Using the property of coefficients.

The idea is to use the property of pascal’s triangle that its coefficients and are nothing but the binomial coefficients so we will calculate binomial coefficients o...read more

Saurabh Kumar
1y
currently not working
 answer = [] for row in range(numRows): ans = 1 ansRow = [1] for col in range(row): ans *= row - col ans //= col + 1 ansRow.append(ans) answer.append(ansRow) return answer
Add answer anonymously...
Capgemini Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter