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++)
🧑💻 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