VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-possible-paths-top-left-bottom-right-nxm-matrix/

⇱ Count Unique Paths in a Grid - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count Unique Paths in a Grid

Last Updated : 12 Jun, 2026

Given two integers m and n representing the number of rows and columns of a grid, respectively, find the number of distinct paths from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1). From any cell, you can move only right or down.

Note: The answer is guaranteed to fit within a 32-bit integer.

Input: m = 2, n = 3
Output: 3
Explanation: There are three distinct paths from the top-left cell to the bottom-right cell.

👁 asas_a

Input: m = 1, n = 4
Output: 1
Explanation: There is only one possible path from the top-left cell to the bottom-right cell.

👁 asx_ax

[Naive Approach] Using Recursion - O(2^(m+n)) Time and O(m+n) Space

The idea is to start from the top-left cell and recursively explore all possible paths to reach the bottom-right cell. From any cell, we can move either down or right. The total number of paths from the current cell is the sum of the paths obtained by making these two moves.

Base Cases:

  • If i == m - 1 and j == n - 1, we have reached the destination cell, so return 1.
  • If i >= m or j >= n, the current position is outside the grid, so return 0.

The recurrence relation will be: solve(i, j, m, n) = solve(i + 1, j, m, n) + solve(i, j + 1, m, n)


Output
3

[Better Approach] Using DP - O(m*n) Time and O(n) Space

The above recursive solution has overlapping subproblems and can be optimized using Dynamic Programming.

Since the number of paths to a cell depends only on the cell directly above it and the cell to its left, we do not need to store the entire m × n DP table.

We use a 1D array dp[], where dp[j] stores the number of paths to reach the current cell in the current row. Initially, there is exactly one way to reach every cell in the first row, so the array is initialized with 1s.

Traverse the grid row by row and update the array from left to right:

  • dp[j] represents the number of paths from the cell above.
  • dp[j - 1] represents the number of paths from the cell to the left.
  • The sum of these two values gives the number of paths to the current cell.

The recurrence relation is: dp[j] = dp[j] + dp[j - 1]


Output
3

[Expected Approach] Using Combinatorics - O(min(m, n)) Time and O(1) Space

Instead of computing paths for every cell, we can directly count the number of valid paths using combinatorics. Every path from the top-left cell to the bottom-right cell consists of a fixed number of right and down moves.

To reach cell (m-1, n-1) from (0, 0):

  • We must make exactly (m - 1) down moves.
  • We must make exactly (n - 1) right moves.

Therefore, the total number of moves is: (m - 1) + (n - 1) = m + n - 2 .

Each unique path corresponds to a unique arrangement of these moves. So, the problem reduces to choosing positions for either:

  • (m - 1) down moves among (m + n - 2) total moves, or
  • (n - 1) right moves among (m + n - 2) total moves.

Hence, the answer is: C(m + n - 2, m - 1) or equivalently, C(m + n - 2, n - 1)


Output
3
Comment