AlgoMaster Logo

Pascal's Triangle

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We need to build Pascal's triangle row by row. Each row starts and ends with 1, and every element in between is the sum of the two elements directly above it from the previous row.

Row 0 is [1]. Row 1 is [1, 1]. For row 2, the edges are 1, and the middle element is 1 + 1 = 2, giving [1, 2, 1]. Row 3 takes the middle values from row 2: 1 + 2 = 3 and 2 + 1 = 3, producing [1, 3, 3, 1].

Row i has exactly i + 1 elements, and element j in row i (where j is not the first or last) equals row[i-1][j-1] + row[i-1][j]. This dependency on the previous row is what drives the solution.

Key Constraints:

  • 1 <= numRows <= 30 → With numRows at most 30, the total number of elements is at most 465. Even an O(n^3) approach would be trivially fast. There is no performance pressure here.
  • The output is a 2D list of integers, so we need to build the triangle as a list of lists.
  • Values stay within 32-bit integer range. The largest value in the triangle is C(29, 14) = 77,558,760, well below the int32 limit of about 2.1 billion.

Approach 1: Iterative Row-by-Row Construction

Intuition

Build Pascal's triangle the same way you'd draw it by hand. Start with the first row [1], then generate each subsequent row from the one before it.

For each new row, the first and last elements are always 1. Each interior element is the sum of the two values directly above it in the previous row. A single nested loop produces the whole triangle.

Algorithm

  1. Initialize the result with the first row [[1]].
  2. For each row index i from 1 to numRows - 1:
    • Create a new row starting with [1].
    • For each position j from 1 to i - 1, compute the value as previousRow[j-1] + previousRow[j] and append it.
    • Append 1 at the end of the row.
    • Add the completed row to the result.
  3. Return the result.

Example Walkthrough

1Row 0: Base case, just [1]
0
1
1/6

Code

This approach is optimal in time, since the output alone contains O(n^2) elements. The next approach reaches the same complexity through a different route: it computes each row from a closed-form binomial formula, without referencing the previous row.

Approach 2: Mathematical (Combinatorial) Construction

Intuition

Every element in Pascal's triangle is a binomial coefficient. Specifically, element j in row i equals C(i, j) = i! / (j! * (i-j)!).

Computing factorials directly would be wasteful and risk overflow, since intermediate factorials grow far faster than the final coefficients. Instead, derive each element from the one before it in the same row using a ratio:

C(i, j) = C(i, j-1) * (i - j + 1) / j

Each row starts with 1 (since C(i, 0) = 1), and multiplying by this ratio produces the next element. The previous row never enters the computation.

Algorithm

  1. Initialize the result list.
  2. For each row index i from 0 to numRows - 1:
    • Start the row with [1].
    • For each position j from 1 to i, compute the next value as previousValue * (i - j + 1) / j.
    • Add the completed row to the result.
  3. Return the result.

Example Walkthrough

1Row 0: C(0,0) = 1 → [1]
0
1
1/6

Code