Java Pattern Program · OT32
Print Pascal's Triangle in Java
Every row starts and ends with 1, and every inner value is the sum of the two values just above it.
Problem Statement
Print the first rows rows of Pascal's triangle. Row r (starting at 0) contains r + 1 values.
Values are separated by single spaces. They are stored as long, because they grow quickly.
Input/Output Examples
rows = 4Output1 / 1 1 / 1 2 1 / 1 3 3 1Explanation: each inner value adds the two above it.row index 5Output1 5 10 10 5 1Explanation: the coefficients of (a + b)5.rows = 1Output1Explanation: just the top.Visual Explanation
| k=0 | k=1 | k=2 | k=3 | k=4 | |
|---|---|---|---|---|---|
| row 0 | 1 | ||||
| row 1 | 1 | 1 | |||
| row 2 | 1 | 2 | 1 | ||
| row 3 | 1 | 3 | 3 | 1 | |
| row 4 | 1 | 4 | 6 | 4 | 1 |
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.
Approach 1: Build From the Previous Row (Recommended)
Idea: each inner value is above[k - 1] + above[k]; the ends are 1.
- For each row, put 1 at both ends.
- Fill the inner positions from the previous row.
- Add the row to the triangle.
Time: O(n²) · Space: O(n²).
Approach 2: Binomial Coefficient Formula
Idea: compute C(r, k + 1) = C(r, k) × (r - k) / (k + 1) from left to right.
- Start each row with
value = 1. - Multiply by
r - kand divide byk + 1to get the next value. - The division is always exact.
Time: O(n²) · Space: O(n). Only the current row is needed.
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.
Approach 1: Build From the Previous Row (Recommended)
Mirrors the definition and only uses addition.
import java.util.ArrayList;
import java.util.List;
public class PascalFromPrevious {
// Each inner value is the sum of the two values above it
static List<List<Long>> pascal(int rows) {
List<List<Long>> triangle = new ArrayList<>();
for (int r = 0; r < rows; r++) {
List<Long> row = new ArrayList<>();
for (int k = 0; k <= r; k++) {
if (k == 0 || k == r) {
row.add(1L);
} else {
List<Long> above = triangle.get(r - 1);
row.add(above.get(k - 1) + above.get(k));
}
}
triangle.add(row);
}
return triangle;
}
public static void main(String[] args) {
for (List<Long> row : pascal(6)) {
StringBuilder sb = new StringBuilder();
for (long v : row) {
sb.append(sb.length() > 0 ? " " : "").append(v);
}
System.out.println(sb);
}
}
}
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Time complexity: O(n²) · Space complexity: O(n²).
Approach 2: Binomial Coefficient Formula
Any single row can be produced without the rows above it.
import java.util.ArrayList;
import java.util.List;
public class PascalFormula {
// C(r, k) = C(r, k - 1) * (r - k + 1) / k, so each row is computed on its own
static List<List<Long>> pascal(int rows) {
List<List<Long>> triangle = new ArrayList<>();
for (int r = 0; r < rows; r++) {
List<Long> row = new ArrayList<>();
long value = 1;
for (int k = 0; k <= r; k++) {
row.add(value);
value = value * (r - k) / (k + 1); // exact: the division always comes out even
}
triangle.add(row);
}
return triangle;
}
public static void main(String[] args) {
for (List<Long> row : pascal(6)) {
StringBuilder sb = new StringBuilder();
for (long v : row) {
sb.append(sb.length() > 0 ? " " : "").append(v);
}
System.out.println(sb);
}
}
}
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Time complexity: O(n²) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Build From the Previous Row | O(n²) | O(n²) |
| Binomial Coefficient Formula | O(n²) | O(n) |
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 theintrange around row 34. - Multiply before dividing in the formula so the division stays exact.
- Know the connection: row
ngives 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.