> 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/dynamic-programming/grid-unique-paths.md).

# Grid Unique Paths

A robot is located at the top-left corner of an **A x B grid** (marked ‘Start’ in the diagram below).

![Path Sum: Example 1](http://i.imgur.com/3eaivQ5.png)

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

*Note: A and B will be such that the resulting answer fits in a 32 bit signed integer.*

**Example :**

```
Input : A = 2, B = 2
Output : 2

2 possible routes : (0, 0) -> (0, 1) -> (1, 1) 
              OR  : (0, 0) -> (1, 0) -> (1, 1)
```

```java
class Solution {
    public int uniquePaths(int m, int n) {
        int dp[][] = new int[m + 1][n + 1];
        dp[m][n] = 1;
        for (int r = m; r >= 1; r--) {
            for (int c = n; c >= 1; c--) {
                if (r == m && c == n)
                    continue;
                else if (c == n)
                    dp[r][c] = dp[r + 1][c];
                else if (r == m)
                    dp[r][c] = dp[r][c + 1];
                else
                    dp[r][c] = dp[r + 1][c] + dp[r][c + 1];
            }
        }
        return dp[1][1];
    }
}
```


---

# 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/dynamic-programming/grid-unique-paths.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.
