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 ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code
def canReach ( X , Y ) : NEW_LINE INDENT steps = 0 NEW_LINE while ( X != 0 or Y != 0 ) : NEW_LINE INDENT pos1 = X % 3 NEW_LINE pos2 = Y % 3 NEW_LINE if ( pos1 == 2 or pos2 == 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( pos1 == 1 and pos2 == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( pos1 == 0 and pos2 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT X /= 3 NEW_LINE Y /= 3 NEW_LINE steps += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT X = 10 NEW_LINE Y = 30 NEW_LINE if ( canReach ( X , Y ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT