Counts paths from a point to reach Origin : Memory Optimized
Last Updated :
24 Mar, 2023
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 down.
Examples:
Input: 3 6
Output: Number of Paths 84
Input: 3 3
Output: 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++
#include <iostream>
#include<cstring>
using namespace std;
long Count_Paths( int x, int y)
{
if (x == 0 && y == 0)
return 0;
if (x == 0 || y == 0)
return 1;
long dp[max(x,y)+1],
p=max(x,y),q=min(x,y);
for ( int i=0;i<=p;i++)
dp[i]=1;
for ( int i=1;i<=q;i++)
for ( int j=1;j<=p;j++)
dp[j]+=dp[j-1];
return dp[p];
}
int main()
{
int x = 3,y = 3;
cout<< "Number of Paths "
<< Count_Paths(x,y);
return 0;
}
|
Java
class GFG {
static long Count_Paths( int x, int y) {
if (x == 0 && y == 0 )
return 0 ;
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);
for ( int i = 0 ; i <= p; i++)
dp[i] = 1 ;
for ( int i = 1 ; i <= q; i++)
for ( int j = 1 ; j <= p; j++)
dp[j] += dp[j - 1 ];
return dp[p];
}
public static void main(String[] args) {
int x = 3 , y = 3 ;
System.out.print( "Number of Paths " + Count_Paths(x, y));
}
}
|
Python3
def Count_Paths(x, y):
if (x = = 0 and y = = 0 ):
return 0
if (x = = 0 and y = = 0 ):
return 1
dp = [ 0 ] * ( max (x, y) + 1 )
p = max (x, y)
q = min (x, y)
for i in range (p + 1 ):
dp[i] = 1
for i in range ( 1 , q + 1 ):
for j in range ( 1 , p + 1 ):
dp[j] + = dp[j - 1 ]
return dp[p]
x = 3
y = 3
print ( "Number of Paths " , Count_Paths(x, y))
|
C#
using System;
class GFG {
static long Count_Paths( int x, int y) {
if (x == 0 && y == 0)
return 0;
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);
for ( int i = 0; i <= p; i++)
dp[i] = 1;
for ( int i = 1; i <= q; i++)
for ( int j = 1; j <= p; j++)
dp[j] += dp[j - 1];
return dp[p];
}
public static void Main(String[] args) {
int x = 3, y = 3;
Console.Write( "Number of Paths " + Count_Paths(x, y));
}
}
|
Javascript
function count_paths(x, y){
if (x == 0 && y == 0){
return 0;
}
if (x == 0 || y == 0){
return 1;
}
let dp = [];
let p = Math.max(x, y);
let q = Math.min(x, y);
for (let i = 0; i <= Math.max(x, y); i++){
if (i <= p){
dp[i] = 1;
}
else {
dp[i] = 0;
}
}
for (let i = 1; i <=q; i++){
for (let j = 1; j <= q; j++){
dp[j] += dp[j-1];
}
}
return dp[p];
}
let x = 3;
let y = 3;
console.log( "Number of Paths " ,count_paths(x, y));
|
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).
- Auxiliary Space: 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))
Share your thoughts in the comments
Please Login to comment...