Check if it is possible to reach any point on the circumference of a given circle from origin | Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , 'R ; Stores the count of ' U ' , 'D ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code
def isPossible ( S , R , N ) : NEW_LINE ' NEW_LINE INDENT cntl = 0 NEW_LINE cntr = 0 NEW_LINE DEDENT ' NEW_LINE INDENT cntu = 0 NEW_LINE cntd = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' L ' ) : NEW_LINE INDENT cntl += 1 NEW_LINE DEDENT elif ( S [ i ] == ' R ' ) : NEW_LINE INDENT cntr += 1 NEW_LINE DEDENT elif ( S [ i ] == ' U ' ) : NEW_LINE INDENT cntu += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntd += 1 NEW_LINE DEDENT DEDENT if ( max ( max ( cntl , cntr ) , max ( cntu , cntd ) ) >= R ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT mp = { } NEW_LINE r_square = R * R NEW_LINE i = 1 NEW_LINE while i * i <= r_square : NEW_LINE INDENT mp [ i * i ] = i NEW_LINE if ( ( r_square - i * i ) in mp ) : NEW_LINE INDENT if ( max ( cntl , cntr ) >= mp [ r_square - i * i ] and max ( cntu , cntd ) >= i ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT if ( max ( cntl , cntr ) >= i and max ( cntu , cntd ) >= mp [ r_square - i * i ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " RDLLDDLDU " NEW_LINE R = 5 NEW_LINE N = len ( S ) NEW_LINE print ( isPossible ( S , R , N ) ) NEW_LINE DEDENT