00:00

Problem Statement

Input/Output Examples

Visual Explanation

Row r lists the binomial coefficients C(r, 0) to C(r, r), so a row can also be computed on its own with the multiplicative formula.

Approaches

Both approaches produce the same rows. One adds values from the previous row; the other computes each coefficient directly.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of rows. The triangle has n(n + 1) / 2 values, each computed in constant time.

Interview Tips and Recommended Approach

  • Use long. Values pass the int range around row 34.
  • Multiply before dividing in the formula so the division stays exact.
  • Know the connection: row n gives the coefficients of (a + b)n.
  • Save memory by updating a single row from right to left.

Recommended Approach: Build From the Previous Row

Why: it follows the definition of the triangle exactly and needs only additions, so there is nothing to round or overflow early.