Counts paths from a point to reach Origin : Memory Optimized | 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
def Count_Paths ( x , y ) : NEW_LINE INDENT if ( x == 0 and y == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x == 0 and y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT dp = [ 0 ] * ( max ( x , y ) + 1 ) NEW_LINE p = max ( x , y ) NEW_LINE q = min ( x , y ) NEW_LINE for i in range ( p + 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , q + 1 ) : NEW_LINE INDENT for j in range ( 1 , p + 1 ) : NEW_LINE INDENT dp [ j ] += dp [ j - 1 ] NEW_LINE DEDENT DEDENT return dp [ p ] NEW_LINE DEDENT x = 3 NEW_LINE y = 3 NEW_LINE print ( " Number ▁ of ▁ Paths ▁ " , Count_Paths ( x , y ) ) NEW_LINE