> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/kth-row-of-pascals-triangle.md).

# Kth Row of Pascal's Triangle

Given an index k, return the kth row of the Pascal’s triangle.

Pascal’s triangle : To generate A\[C] in row R, sum up A’\[C] and A’\[C-1] from previous row R - 1.

**Example:**

```
Input : k = 3

Return : [1,3,3,1]
```

> **NOTE** : k is 0 based. k = 0, corresponds to the row \[1].&#x20;

*Note:Could you optimize your algorithm to use only O(k) extra space?*

```java
public class Solution {
    public int[] getRow(int A) {
        int[] ans = new int[A + 1];
        int nC1 = A;
        int r = 1;
        for (int i = 0; i <= A; i++) {
            if (i == 0 || i == A)
                ans[i] = 1;
            else {
                ans[i] = nC1;
                nC1 = nC1 * (A - r) / (1 + r);
                r++;
            }
        }
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/kth-row-of-pascals-triangle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
