Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code
def canReach ( X , Y , steps ) : NEW_LINE INDENT if ( X == 0 and Y == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( X < 0 or Y < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( canReach ( X - int ( pow ( 3 , steps ) ) , Y , steps + 1 ) | canReach ( X , Y - int ( pow ( 3 , steps ) ) , steps + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 10 NEW_LINE Y = 30 NEW_LINE if ( canReach ( X , Y , 0 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT