- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a number n, we have to find the nth (0-indexed) row of Pascal's triangle. As we know the Pascal's triangle can be created as follows −

- In the top row, there is an array of 1.
- Subsequent row is made by adding the number above and to the left with the number above and to the right.

So few rows are as follows −

So, if the input is like 4, then the output will be [1, 4, 6, 4, 1]

To solve this, we will follow these steps −

- if n is same as 0, then
- return [1]

- if n is same as 1, then
- return [1,1]

- ls:= a list with [1,1], temp:= a list with [1,1]
- for i in range 2 to n+1, do
- ls:= temp
- temp:= a list with one value = 1
- for i in range 0 to size of ls -1, do
- merge ls[i],ls[i+1] and insert at the end of temp

- insert 1 at the end of temp

- return temp

Let us see the following implementation to get better understanding −

class Solution: def solve(self, n): if n==0: return [1] if n==1: return [1,1] ls=[1,1] temp=[1,1] for i in range(2,n+1): ls=temp temp=[1] for i in range(len(ls)-1): temp.append(ls[i]+ls[i+1]) temp.append(1) return temp ob = Solution() print(ob.solve(4))

4

[1, 4, 6, 4, 1]

- Related Questions & Answers
- Finding the elements of nth row of Pascal's triangle in JavaScript
- Java program to print Pascal's triangle
- Pascal's Triangle in C++
- Pascal's Triangle II in C++
- C# Program to create Pascal’s Triangle
- Python Program to Print the Pascal's triangle for n number of rows given by the user
- Python program using map function to find row with maximum number of 1's
- Python program using the map function to find a row with the maximum number of 1's
- Java Program to calculate the area of a triangle using Heron's Formula
- How to print integers in the form of Pascal triangle using C?
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Program to find minimum number of characters to be deleted to make A's before B's in Python
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Program to find the Centroid of the Triangle in C++

Advertisements