Counts paths from a point to reach Origin
You are standing on a point (n, m) and you want to go to origin (0, 0) by taking steps either left or down i.e. from each point you are allowed to move either in (n-1, m) or (n, m-1). Find the number of paths from point to origin.
Examples:
Input : 3 6 Output : Number of Paths 84 Input : 3 0 Output : Number of Paths 1


As we are restricted to move down and left only we would run a recursive loop for each of the combinations of the
steps that can be taken.
// Recursive function to count number of paths
countPaths(n, m)
{
// If we reach bottom or top left, we are
// have only one way to reach (0, 0)
if (n==0 || m==0)
return 1;
// Else count sum of both ways
return (countPaths(n-1, m) + countPaths(n, m-1));
}
Below is the implementation of above steps.
C++
// CPP program to count total number of // paths from a point to origin #include<bits/stdc++.h> using namespace std; // Recursive function to count number of paths int countPaths(int n, int m) { // If we reach bottom or top left, we are // have only one way to reach (0, 0) if (n==0 || m==0) return 1; // Else count sum of both ways return (countPaths(n-1, m) + countPaths(n, m-1)); } // Driver Code int main() { int n = 3, m = 2; cout << " Number of Paths " << countPaths(n, m); return 0; } |
Java
// Java program to count total number of // paths from a point to origin import java.io.*; class GFG { // Recursive function to count number of paths static int countPaths(int n, int m) { // If we reach bottom or top left, we are // have only one way to reach (0, 0) if (n == 0 || m == 0) return 1; // Else count sum of both ways return (countPaths(n - 1, m) + countPaths(n, m - 1)); } // Driver Code public static void main (String[] args) { int n = 3, m = 2; System.out.println (" Number of Paths " + countPaths(n, m)); } } // This code is contributed by vt_m |
Python3
# Python program to count # total number of # paths from a point to origin # Recursive function to # count number of paths def countPaths(n,m): # If we reach bottom # or top left, we are # have only one way to reach (0, 0) if (n==0 or m==0): return 1 # Else count sum of both ways return (countPaths(n-1, m) + countPaths(n, m-1)) # Driver Code n = 3m = 2print(" Number of Paths ", countPaths(n, m)) # This code is contributed # by Azkia Anam. |
C#
// C# program to count total number of // paths from a point to origin using System; public class GFG { // Recursive function to count number // of paths static int countPaths(int n, int m) { // If we reach bottom or top left, // we are have only one way to // reach (0, 0) if (n == 0 || m == 0) return 1; // Else count sum of both ways return (countPaths(n - 1, m) + countPaths(n, m - 1)); } // Driver Code public static void Main () { int n = 3, m = 2; Console.WriteLine (" Number of" + " Paths " + countPaths(n, m)); } } // This code is contributed by Sam007. |
PHP
<?php // PHP program to count total number // of paths from a point to origin // Recursive function to // count number of paths function countPaths($n, $m) { // If we reach bottom or // top left, we are // have only one way to // reach (0, 0) if ($n == 0 || $m == 0) return 1; // Else count sum of both ways return (countPaths($n - 1, $m) + countPaths($n, $m - 1)); } // Driver Code $n = 3; $m = 2; echo " Number of Paths " , countPaths($n, $m); // This code is contributed by aj_36 ?> |
Output:
Number of Paths 10
We can use Dynamic Programming as there are overlapping subproblems. We can draw recursion tree to see overlapping problems. For example, in case of countPaths(4, 4), we compute countPaths(3, 3) multiple times.
C++
// CPP program to count total number of // paths from a point to origin #include<bits/stdc++.h> using namespace std; // DP based function to count number of paths int countPaths(int n, int m) { int dp[n+1][m+1]; // Fill entries in bottommost row and leftmost // columns for (int i=0; i<=n; i++) dp[i][0] = 1; for (int i=0; i<=m; i++) dp[0][i] = 1; // Fill DP in bottom up manner for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) dp[i][j] = dp[i-1][j] + dp[i][j-1]; return dp[n][m]; } // Driver Code int main() { int n = 3, m = 2; cout << " Number of Paths " << countPaths(n, m); return 0; } |
Java
// Java program to count total number of // paths from a point to origin import java.io.*; class GFG { // DP based function to count number of paths static int countPaths(int n, int m) { int dp[][] = new int[n + 1][m + 1]; // Fill entries in bottommost row and leftmost // columns for (int i = 0; i <= n; i++) dp[i][0] = 1; for (int i = 0; i <= m; i++) dp[0][i] = 1; // Fill DP in bottom up manner for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; return dp[n][m]; } // Driver Code public static void main (String[] args) { int n = 3, m = 2; System.out.println(" Number of Paths " + countPaths(n, m)); } } // This code is contributed by vt_m |
Python 3
# Python3 program to count total # number of paths from a po to origin # Recursive function to count # number of paths def countPaths(n, m): # If we reach bottom or top # left, we are have only one # way to reach (0, 0) if (n == 0 or m == 0): return 1 # Else count sum of both ways return (countPaths(n - 1, m) + countPaths(n, m - 1)) # Driver Code n = 3m = 2print("Number of Paths", countPaths(n, m)) # This code is contributed by ash264 |
C#
// C# program to count total number of // paths from a point to origin using System; public class GFG { // DP based function to count number // of paths static int countPaths(int n, int m) { int [,]dp = new int[n + 1,m + 1]; // Fill entries in bottommost row // and leftmost columns for (int i = 0; i <= n; i++) dp[i,0] = 1; for (int i = 0; i <= m; i++) dp[0,i] = 1; // Fill DP in bottom up manner for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dp[i,j] = dp[i - 1,j] + dp[i,j - 1]; return dp[n,m]; } // Driver Code public static void Main () { int n = 3, m = 2; Console.WriteLine(" Number of" + " Paths " + countPaths(n, m)); } } // This code is contributed by Sam007. |
PHP
<?php // PHP program to count total number of // paths from a point to origin // DP based function to // count number of paths function countPaths($n, $m) { //$dp[$n+1][$m+1]; // Fill entries in bottommost // row and leftmost columns for ($i = 0; $i <= $n; $i++) $dp[$i][0] = 1; for ($i = 0; $i <= $m; $i++) $dp[0][$i] = 1; // Fill DP in bottom up manner for ($i = 1; $i <= $n; $i++) for ($j = 1; $j <= $m; $j++) $dp[$i][$j] = $dp[$i - 1][$j] + $dp[$i][$j - 1]; return $dp[$n][$m]; } // Driver Code $n = 3; $m = 2; echo " Number of Paths " , countPaths($n, $m); // This code is contributed by m_kit ?> |
Output:
Number of Paths 10
This article is contributed by Nikhil Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Minimum cost to reach a point N from 0 with two different operations allowed
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Counts Path in an Array
- Queries for counts of multiples in an array
- Queries for counts of array elements with values in given range
- Minimum operations to make counts of remainders same in an array
- Generate original array from an array that store the counts of greater elements on right
- Number of paths with exactly k coins
- Number of Paths of Weight W in a K-ary tree
- Unique paths in a Grid with Obstacles
- Count number of paths with at-most k turns
- Number of palindromic paths in a matrix
- Number of different cyclic paths of length N in a tetrahedron
- Print all possible paths from top left to bottom right of a mXn matrix
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally



