Counts paths from a point to reach Origin : Memory Optimized | C ++ implementation to count the paths from a points to origin ; Function to count the paths from a point to the origin ; Base Case when the point is already at origin ; Base Case when the point is on the x or y - axis ; Loop to fill all the position as 1 in the array ; Loop to count the number of paths from a point to origin in bottom - up manner ; Driver Code ; Function Call
#include <iostream> NEW_LINE #include <cstring> NEW_LINE 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 ; }