Solution Pascal's Triangle
Problem Statement:
Write a Recursive Solution to Generate Pascal's Triangle.
Write a recursive function to generate Pascal's Triangle up to a given number of rows. Pascal's Triangle is a triangular array of binomial coefficients, where each number is the sum of the two numbers directly above it.
Example:
| Sr# | Input | Output | Explanation |
|---|---|---|---|
| 1 | numRows = 3 | [[1],[1,1],[1,2,1]] | The first 3 rows of Pascal's Triangle are [[1],[1,1],[1,2,1]]. |
| 2 | numRows = 5 | [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] | The first 5 rows of Pascal's Triangle are [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]. |
| 3 | numRows = 1 | [[1]] | The first row of Pascal's Triangle is [[1]]. |
Constraints:
1 <= numRows <= 30
Solution:
- Base Case:
- If the number of rows (numRows) is 0, return an empty list ([]), as there are no rows to generate.
- Recursive Case:
- If the number of rows is 1, return a list with a single sublist containing the number 1 ([1]), as it is the first row of Pascal's Triangle.
- For each row greater than 1, recursively generate the previous row and use it to calculate the current row.
- The current row is generated by adding adjacent elements from the previous row and appending 1 at the beginning and end.
- Repeat the process until the desired number of rows is reached.
This approach works by breaking down the problem into smaller subproblems. The base case handles the termination condition when there are no rows to generate. The recursive case leverages the previously generated row to calculate the current row, gradually building Pascal's Triangle row by row.
Code
Here is the code for this algorithm:
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<List<Integer>> generatePascalTriangle(int numRows) {
// Base Case: No rows to generate
if (numRows == 0) {
return new ArrayList<>();
}
// Base Case: First row of Pascal's Triangle
if (numRows == 1) {
List<Integer> row = new ArrayList<>();
row.add(1);
List<List<Integer>> triangle = new ArrayList<>();
triangle.add(row);
return triangle;
}
// Recursive Case: Generate previous row and calculate current row
List<List<Integer>> triangle = generatePascalTriangle(numRows - 1);
List<Integer> prevRow = triangle.get(numRows - 2);
List<Integer> currRow = new ArrayList<>();
currRow.add(1);
for (int i = 1; i < numRows - 1; i++) {
int sum = prevRow.get(i - 1) + prevRow.get(i);
currRow.add(sum);
}
currRow.add(1);
triangle.add(currRow);
return triangle;
}
public static void main(String[] args) {
int numRows = 5;
List<List<Integer>> pascalTriangle = generatePascalTriangle(numRows);
System.out.println("Pascal's Triangle for " + numRows + " rows:");
for (List<Integer> row : pascalTriangle) {
System.out.println(row);
}
}
}
Time Complexity:
The time complexity of this algorithm is
Space Complexity:
The space complexity of this algorithm is
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Pascal's Triangle? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Pascal's Triangle** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Pascal's Triangle** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Pascal's Triangle**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Pascal's Triangle**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.