Pascal’s Triangle

Prem Parmar
Competitive Programming Problems
2 min readJul 23, 2022

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Ref : https://leetcode.com/problems/pascals-triangle/

If we look pascal triangle in the form of rectangular shape or matrix then it’ll be more understandable.

In any programming language, we make pattern using loops and traversing from rows to column. Generally, we take i = row & j = column.

You can also get Pascal’s triangle by formula. But if we consider this thing as a pattern then It’s sum of its above element and above left element.

Current Element = Above Element + Above Left Element

In case element is not there, then consider it as ZERO (0).

You can check below code for more understanding. We have added two default rows 1, 11. If we want pascal triangle for more than that then we’ll use that rows.

If You want to know why first and second rows is pascal’s triangle is 1, 11, Please check this.

That’s it for the today. If you have any doubt, feel free to raise query via comment section.

--

--

Prem Parmar
Competitive Programming Problems

Software Engineer, having 3 years of experience in Ecommerce / HCM domain Product based company.