




Sponsored
Sponsored
This approach involves generating Pascal's Triangle row by row using an iterative method. The first row is initialized with [1], and for each subsequent row, the first and last element are always 1. The intermediate elements are calculated as sums of appropriate elements from the previous row.
Time Complexity: O(numRows^2) due to the nested loop structure.
Space Complexity: O(numRows^2) as we store all elements of the triangle.
1#include <stdio.h>
2#include <stdlib.h>
3
4int** generate(int numRows, int* returnSize, int** returnColumnSizes) {
5    *returnSize = numRows;
6    *returnColumnSizes = (int*)malloc(numRows * sizeof(int));
7    int** triangle = (int**)malloc(numRows * sizeof(int*));
8    for (int i = 0; i < numRows; i++) {
9        triangle[i] = (int*)malloc((i + 1) * sizeof(int));
10        (*returnColumnSizes)[i] = i + 1;
11        triangle[i][0] = 1;
12        triangle[i][i] = 1;
13        for (int j = 1; j < i; j++) {
14            triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
15        }
16    }
17    return triangle;
18}
19
20int main() {
21    int returnSize;
22    int* returnColumnSizes;
23    int numRows = 5;
24    int** result = generate(numRows, &returnSize, &returnColumnSizes);
25    for (int i = 0; i < returnSize; i++) {
26        for (int j = 0; j < returnColumnSizes[i]; j++) {
27            printf("%d ", result[i][j]);
28        }
29        printf("\n");
30        free(result[i]);
31    }
32    free(result);
33    free(returnColumnSizes);
34    return 0;
35}
36In this C implementation, we allocate a 2D array to hold the triangle's content. We initialize the first and last entries of each row to 1. For each intermediate row entry, we compute its value as the sum of the two appropriate values from the preceding row, incrementally building each row from top to bottom.
This approach constructs Pascal's Triangle using recursion by calculating each value based on the combination formula. The values on the edges are always 1, and other values are calculated as combinatorial numbers derived recursively.
Time Complexity: O(numRows^3) due to recursive calls, not optimal.
Space Complexity: O(numRows^2) for storing the triangle.
1#include <iostream>
2#include <vector>
int combination(int n, int k) {
    if (k == 0 || k == n) return 1;
    return combination(n - 1, k - 1) + combination(n - 1, k);
}
std::vector<std::vector<int>> generate(int numRows) {
    std::vector<std::vector<int>> triangle(numRows);
    for (int i = 0; i < numRows; i++) {
        triangle[i] = std::vector<int>(i + 1);
        for (int j = 0; j <= i; j++) {
            triangle[i][j] = combination(i, j);
        }
    }
    return triangle;
}
int main() {
    int numRows = 5;
    std::vector<std::vector<int>> result = generate(numRows);
    for (const auto& row : result) {
        for (int value : row) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}
This C++ approach defines a recursive function that calculates each entry according to Pascal's combination principles. Recursive function calls compute combination values used in constructing each row incrementally.