1. Pascal Triangle

The problem can be found at the following link: ๐Ÿ”— Question Link

๐Ÿงฉ Problem Description

Given a positive integer n, return the nth row of Pascal's Triangle. Pascal's Triangle is a triangular array where each element is the sum of the two elements directly above it in the previous row. The nth row contains n elements indexed from 0 to n-1, and follows the binomial coefficient formula: row[k] = C(n-1, k)

๐Ÿ“˜ Examples

Example 1:

Input:

n = 4

Output:

[1, 3, 3, 1]

Explanation:

The 4th row of Pascalโ€™s Triangle is [1, 3, 3, 1].

Example 2:

Input:

n = 5

Output:

[1, 4, 6, 4, 1]

Explanation:

The 5th row of Pascalโ€™s Triangle is [1, 4, 6, 4, 1].

Example 3:

Input:

n = 1

Output:

[1]

Explanation:

The 1st row of Pascalโ€™s Triangle is [1].

๐Ÿ”’ Constraints

  • $1 \leq n \leq 20$

โœ… My Approach

Optimized Approach: Direct Binomial Formula

We use the mathematical identity: C(n, k) = C(n, k - 1) * (n - k + 1) / k

$[ \text{row}[0] = 1,\quad \text{row}[i] = \text{row}[i-1] \times \frac{n-1 - (i-1)}{i}\quad \text{for } i=1,\dots,n-1 ]$

This builds the row in a single pass, using only integer arithmetic and avoiding overflow when $n\le20$.

Algorithm Steps:

  1. Initialize result list row = [].

  2. Set variable val = 1 (first element is always 1).

  3. For each index k from 0 to n-1:

    • Append current val to row.

    • Compute next val as: val = val * (n - 1 - k) / (k + 1)

  4. Return the row.

๐Ÿงฎ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we compute each of the n elements once using constant-time math.

  • Expected Auxiliary Space Complexity: O(n), as we store exactly n integers in the result list.

๐Ÿง  Code (C++)

class Solution {
  public:
    vector<int> nthRowOfPascalTriangle(int n) {
        vector<int> row(n);
        row[0]=1;
        for(int i=1;i<n;++i) row[i]=row[i-1]*(n-i)/i;
        return row;
    }
};
โšก Alternative Approaches

๐Ÿ“Š 2๏ธโƒฃ Build from Previous Row

Algorithm Steps:

  1. Initialize a vector prev = {1}.

  2. For each level i from 2 to n:

    • Create a new curr of size i, set curr[0]=curr[i-1]=1.

    • For j in [1..i-2], curr[j] = prev[j-1] + prev[j].

    • Swap prev and curr.

  3. Return prev.

class Solution {
  public:
    vector<int> nthRowOfPascalTriangle(int n) {
        vector<int> prev={1}, curr;
        for(int i=2;i<=n;++i){
            curr.assign(i,1);
            for(int j=1;j<i-1;++j) curr[j]=prev[j-1]+prev[j];
            prev.swap(curr);
        }
        return prev;
    }
};

โœ… Why This Approach?

  • Conceptually simple (direct use of Pascalโ€™s definition).

  • No risk of overflow if values small.

๐Ÿ“ Complexity Analysis:

  • Time: O(nยฒ) (summing up all elements up to row n).

  • Auxiliary Space: O(n) (two rows of length โ‰ค n).

๐Ÿ“Š 3๏ธโƒฃ Full Triangle Generation

Algorithm Steps:

  1. Create a 2D vector T of size n ร— n, initialize all to 0.

  2. Set T[i][0]=T[i][i]=1 for each row i.

  3. For each i from 2 to n-1 and j from 1 to i-1, T[i][j]=T[i-1][j-1]+T[i-1][j].

  4. Return T[n-1].

class Solution {
  public:
    vector<int> nthRowOfPascalTriangle(int n) {
        vector<vector<int>> T(n, vector<int>(n));
        for(int i=0;i<n;++i){
            T[i][0]=T[i][i]=1;
            for(int j=1;j<i;++j) T[i][j]=T[i-1][j-1]+T[i-1][j];
        }
        return T[n-1];
    }
};

โœ… Why This Approach?

  • Good for when multiple rows are needed.

  • Directly produces the entire triangle.

๐Ÿ“ Complexity Analysis:

  • Time: O(nยฒ)

  • Auxiliary Space: O(nยฒ)

๐Ÿ“Š 4๏ธโƒฃ Factorialโ€Based Formula

Algorithm Steps:

  1. Precompute factorials fact[k] for k up to n-1.

  2. For each k in [0..n-1], compute C(n-1,k) = fact[n-1] / (fact[k]*fact[n-1-k]).

class Solution {
  public:
    vector<int> nthRowOfPascalTriangle(int n) {
        vector<long long> fact(n);
        fact[0]=1;
        for(int i=1;i<n;++i) fact[i]=fact[i-1]*i;
        vector<int> row(n);
        for(int k=0;k<n;++k)
            row[k]=fact[n-1]/(fact[k]*fact[n-1-k]);
        return row;
    }
};

โœ… Why This Approach?

  • Uses closedโ€form combinatorics.

  • If factorials cached, can answer multiple queries very fast.

๐Ÿ“ Complexity Analysis:

  • Time: O(n) for factorial build + O(n) for row โ†’ O(n)

  • Auxiliary Space: O(n)

๐Ÿ†š Comparison of Approaches

Approach

โฑ๏ธ Time

๐Ÿ—‚๏ธ Space

โœ… Pros

โš ๏ธ Cons

Direct Binomial Formula

๐ŸŸข O(n)

๐ŸŸข O(n)

Fast, no factorials needed

Risk of overflow if n is large

Build from Previous Row

๐Ÿ”ธ O(nยฒ)

๐ŸŸข O(n)

Intuitive, avoids math operations

Slower for larger n

Full Triangle Generation

๐Ÿ”ธ O(nยฒ)

๐Ÿ”ธ O(nยฒ)

Useful if full triangle needed

High memory usage

Factorial-Based

๐ŸŸข O(n)

๐ŸŸข O(n)

Mathematical, supports batch queries

Needs extra space and handling overflow

โœ… Best Choice?

Scenario

Recommended Approach

Single large row, optimal runtime

๐Ÿฅ‡ Direct Formula

Need to inspect multiple rows

๐Ÿฅˆ Full Triangle Generation

Educational/demo purpose

๐Ÿฅ‰ Build from Previous Row

Batch queries with varying n

๐ŸŽ–๏ธ Factorialโ€Based

๐Ÿง‘โ€๐Ÿ’ป Code (Java)

class Solution {
    public ArrayList<Integer> nthRowOfPascalTriangle(int n) {
        ArrayList<Integer> row=new ArrayList<>();
        long v=1;
        for(int i=0;i<n;++i){
            row.add((int)v);
            v=v*(n-1-i)/(i+1);
        }
        return row;
    }
}

๐Ÿ Code (Python)

class Solution:
    def nthRowOfPascalTriangle(self, n):
        row, v = [], 1
        for i in range(n):
            row.append(v)
            v = v * (n-1-i) // (i+1)
        return row

๐Ÿง  Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐Ÿ“ฌ Any Questions?. Letโ€™s make this learning journey more collaborative!

โญ If you find this helpful, please give this repository a star! โญ


๐Ÿ“Visitor Count

Last updated