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:
Initialize result list
row = [].Set variable
val = 1(first element is always 1).For each index
kfrom0ton-1:Append current
valtorow.Compute next
valas:val = val * (n - 1 - k) / (k + 1)
Return the
row.
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we compute each of the
nelements once using constant-time math.Expected Auxiliary Space Complexity: O(n), as we store exactly
nintegers in the result list.
🧠 Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Build from Previous Row
Algorithm Steps:
Initialize a vector
prev = {1}.For each level
ifrom 2 ton:Create a new
currof sizei, setcurr[0]=curr[i-1]=1.For
jin[1..i-2],curr[j] = prev[j-1] + prev[j].Swap
prevandcurr.
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:
Create a 2D vector
Tof sizen × n, initialize all to 0.Set
T[i][0]=T[i][i]=1for each rowi.For each
ifrom 2 ton-1andjfrom 1 toi-1,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:
Precompute factorials
fact[k]forkup ton-1.For each
kin[0..n-1], computeC(n-1,k) = fact[n-1] / (fact[k]*fact[n-1-k]).
✅ 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)
🐍 Code (Python)
🧠 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