ExamAdda Logo

Pascal's Triangle

Easy

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

In Pascal's Triangle:

  • The first row contains 1.
  • Every row starts and ends with 1.
  • Each inner element is the sum of the two elements directly above it.

For example, when numRows = 5:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Example 1

Input

numRows = 5

Output

[
    [1],
    [1, 1],
    [1, 2, 1],
    [1, 3, 3, 1],
    [1, 4, 6, 4, 1]
]

Explanation

The first row contains 1.

Each following row begins and ends with 1. Every inner value is obtained by adding the two values directly above it.

Example 2

Input

numRows = 1

Output

[
    [1]
]

Explanation

When only one row is requested, Pascal's Triangle contains a single 1.

Constraints

1 <= numRows <= 30
Each row must be generated according to Pascal's Triangle.
Return exactly numRows rows.

Hints:

Hint 1

The first and last elements of every row are always 1.

Hint 2

For every inner position:

current[j] = previous[j - 1] + previous[j]

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 26, 2026

Last updated

Aug 26, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output