Pascal's Triangle Row - aloalgo

Pascal's Triangle Row

Medium

Pascal's Triangle Row

Given an integer n, return the n-th row of Pascal's triangle. The rows are 1-indexed, meaning n=1 corresponds to the first non-empty row. If n is 0 or less, an empty array [] should be returned.

In Pascal's triangle, each number is the sum of the two numbers directly above it. The first and last numbers in each row are always 1.

For example:

  • Row 0: [] (by definition for n <= 0)
  • Row 1: [1]
  • Row 2: [1, 1]
  • Row 3: [1, 2, 1]
  • Row 4: [1, 3, 3, 1]
  • Row 5: [1, 4, 6, 4, 1]

Example 1

Input
1
Output
[1]
Explanation:

The 1st row of Pascal's triangle is simply [1].

Example 2

Input
3
Output
[1, 2, 1]
Explanation:

To get the 3rd row, we derive it from the 2nd row [1, 1]. The elements are 1, (1+1)=2, 1.

Example 3

Input
5
Output
[1, 4, 6, 4, 1]
Explanation:

The 5th row is calculated from the 4th row [1, 3, 3, 1]. The elements are 1, (1+3)=4, (3+3)=6, (3+1)=4, 1.

Example 4

Input
0
Output
[]
Explanation:

As per the problem description, for n less than or equal to 0, an empty array should be returned.

Loading...

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!