Given two integers **N** and **M**, which denotes the position of in the coordinate system, the task is to count path from this point to reach origin by taking steps either left or right.**Examples:**

Input:3 6Output:Number of Paths 84Input:3 3Output:Number of Paths 20

We have already discussed this problem in this article –

Counts paths from a point to reach Origin**Approach:** The idea is to use the Dynamic programming paradigm to solve this problem. In this problem, there is a restriction to move only left or down in a path, due to which for any point number of paths is equal to the sum of the number of paths for the left point and the number of paths for the right point and in this way we compute the path for every point in a bottom-up manner from (0, 0) to (N, M). The key observation in this problem to reduce the space is for computing the solution for a point say (i, j) we only need the value of (i-1, j) and (i, j-1), that is we don’t need the portion of (i-1, j-1) computed values once they are used.

Below is the implementation of the above approach:

## C++

`// C++ implementation to count the ` `// paths from a points to origin ` ` ` `#include <iostream> ` `#include<cstring> ` ` ` `using` `namespace` `std; ` ` ` `// Function to count the paths from ` `// a point to the origin ` `long` `Count_Paths(` `int` `x,` `int` `y) ` `{ ` ` ` `// Base Case when the point ` ` ` `// is already at origin ` ` ` `if` `(x == 0 && y == 0) ` ` ` `return` `0; ` ` ` ` ` `// Base Case when the point ` ` ` `// is on the x or y-axis ` ` ` `if` `(x == 0 || y == 0) ` ` ` `return` `1; ` ` ` ` ` `long` `dp[max(x,y)+1], ` ` ` `p=max(x,y),q=min(x,y); ` ` ` ` ` `// Loop to fill all the ` ` ` `// position as 1 in the array ` ` ` `for` `(` `int` `i=0;i<=p;i++) ` ` ` `dp[i]=1; ` ` ` ` ` `// Loop to count the number of ` ` ` `// paths from a point to origin ` ` ` `// in bottom-up manner ` ` ` `for` `(` `int` `i=1;i<=q;i++) ` ` ` `for` `(` `int` `j=1;j<=p;j++) ` ` ` `dp[j]+=dp[j-1]; ` ` ` ` ` `return` `dp[p]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `x = 3,y = 3; ` ` ` ` ` `// Function Call ` ` ` `cout<< ` `"Number of Paths "` ` ` `<< Count_Paths(x,y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count the ` `// paths from a points to origin ` ` ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count the paths from ` ` ` `// a point to the origin ` ` ` `static` `long` `Count_Paths(` `int` `x, ` `int` `y) { ` ` ` `// Base Case when the point ` ` ` `// is already at origin ` ` ` `if` `(x == ` `0` `&& y == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Base Case when the point ` ` ` `// is on the x or y-axis ` ` ` `if` `(x == ` `0` `|| y == ` `0` `) ` ` ` `return` `1` `; ` ` ` ` ` `int` `[] dp = ` `new` `int` `[Math.max(x, y) + ` `1` `]; ` ` ` `int` `p = Math.max(x, y), q = Math.min(x, y); ` ` ` ` ` `// Loop to fill all the ` ` ` `// position as 1 in the array ` ` ` `for` `(` `int` `i = ` `0` `; i <= p; i++) ` ` ` `dp[i] = ` `1` `; ` ` ` ` ` `// Loop to count the number of ` ` ` `// paths from a point to origin ` ` ` `// in bottom-up manner ` ` ` `for` `(` `int` `i = ` `1` `; i <= q; i++) ` ` ` `for` `(` `int` `j = ` `1` `; j <= p; j++) ` ` ` `dp[j] += dp[j - ` `1` `]; ` ` ` ` ` `return` `dp[p]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `x = ` `3` `, y = ` `3` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(` `"Number of Paths "` `+ Count_Paths(x, y)); ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to count the ` `# paths from a points to origin ` ` ` `# Function to count the paths from ` `# a point to the origin ` `def` `Count_Paths(x, y): ` ` ` ` ` `# Base Case when the point ` ` ` `# is already at origin ` ` ` `if` `(x ` `=` `=` `0` `and` `y ` `=` `=` `0` `): ` ` ` `return` `0` ` ` ` ` `# Base Case when the point ` ` ` `# is on the x or y-axis ` ` ` `if` `(x ` `=` `=` `0` `and` `y ` `=` `=` `0` `): ` ` ` `return` `1` ` ` ` ` `dp ` `=` `[` `0` `] ` `*` `(` `max` `(x, y) ` `+` `1` `) ` ` ` ` ` `p ` `=` `max` `(x, y) ` ` ` `q ` `=` `min` `(x, y) ` ` ` ` ` `# Loop to fill all the ` ` ` `# position as 1 in the array ` ` ` `for` `i ` `in` `range` `(p ` `+` `1` `): ` ` ` `dp[i] ` `=` `1` ` ` ` ` `# Loop to count the number of ` ` ` `# paths from a point to origin ` ` ` `# in bottom-up manner ` ` ` `for` `i ` `in` `range` `(` `1` `, q ` `+` `1` `): ` ` ` `for` `j ` `in` `range` `(` `1` `, p ` `+` `1` `): ` ` ` `dp[j] ` `+` `=` `dp[j ` `-` `1` `] ` ` ` ` ` `return` `dp[p] ` ` ` `# Driver Code ` `x ` `=` `3` `y ` `=` `3` ` ` `# Function call ` `print` `(` `"Number of Paths "` `, Count_Paths(x, y)) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count the ` `// paths from a points to origin ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count the paths from ` ` ` `// a point to the origin ` ` ` `static` `long` `Count_Paths(` `int` `x, ` `int` `y) { ` ` ` `// Base Case when the point ` ` ` `// is already at origin ` ` ` `if` `(x == 0 && y == 0) ` ` ` `return` `0; ` ` ` ` ` `// Base Case when the point ` ` ` `// is on the x or y-axis ` ` ` `if` `(x == 0 || y == 0) ` ` ` `return` `1; ` ` ` ` ` `int` `[] dp = ` `new` `int` `[Math.Max(x, y) + 1]; ` ` ` `int` `p = Math.Max(x, y), q = Math.Min(x, y); ` ` ` ` ` `// Loop to fill all the ` ` ` `// position as 1 in the array ` ` ` `for` `(` `int` `i = 0; i <= p; i++) ` ` ` `dp[i] = 1; ` ` ` ` ` `// Loop to count the number of ` ` ` `// paths from a point to origin ` ` ` `// in bottom-up manner ` ` ` `for` `(` `int` `i = 1; i <= q; i++) ` ` ` `for` `(` `int` `j = 1; j <= p; j++) ` ` ` `dp[j] += dp[j - 1]; ` ` ` ` ` `return` `dp[p]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) { ` ` ` `int` `x = 3, y = 3; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(` `"Number of Paths "` `+ Count_Paths(x, y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

Number of Paths 20

**Performance Analysis:**

**Time Complexity:**In the above-given approach, there are two loops of which takes O(N*M) time in the worst case. Therefore, the time complexity for this approach will be**O(N*M)**.**Space Complexity:**In the above-given approach, there is a single array of the size maximum value of N and M. Therefore the space complexity for the above approach will be**O(max(N, M))**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Counts paths from a point to reach Origin
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Sum of cost of all paths to reach a given cell in a Matrix
- Paths requiring minimum number of jumps to reach end of array
- Counts Path in an Array
- Minimum cost to reach a point N from 0 with two different operations allowed
- A Space Optimized Solution of LCS
- A Space Optimized DP solution for 0-1 Knapsack Problem
- Longest Common Substring (Space optimized DP solution)
- Word Wrap problem ( Space optimized solution )
- Number of n digit stepping numbers | Space optimized solution
- Print Longest Bitonic subsequence (Space Optimized Approach)
- Printing Matrix Chain Multiplication (A Space Optimized Solution)
- Count of ways to traverse a Matrix and return to origin in K steps
- Count all possible paths from top left to bottom right of a mXn matrix
- Number of paths with exactly k coins
- Count number of paths with at-most k turns
- Number of palindromic paths in a matrix
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Total number of decreasing paths in a matrix

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.