Check if it is possible to reach any point on the circumference of a given circle from origin | Java program for the above approach ; 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String isPossible ( String S , int R , int N ) { int cntl = 0 , cntr = 0 ; int cntu = 0 , cntd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' L ' ) cntl ++ ; else if ( S . charAt ( i ) == ' R ' ) cntr ++ ; else if ( S . charAt ( i ) == ' U ' ) cntu ++ ; else cntd ++ ; } if ( Math . max ( Math . max ( cntl , cntr ) , Math . max ( cntu , cntd ) ) >= R ) return " Yes " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int r_square = R * R ; for ( int i = 1 ; i * i <= r_square ; i ++ ) { mp . put ( i * i , i ) ; if ( mp . containsKey ( r_square - i * i ) ) { if ( Math . max ( cntl , cntr ) >= mp . get ( r_square - i * i ) && Math . max ( cntu , cntd ) >= i ) return " Yes " ; if ( Math . max ( cntl , cntr ) >= i && Math . max ( cntu , cntd ) >= mp . get ( r_square - i * i ) ) return " Yes " ; } } return " No " ; } public static void main ( String [ ] args ) { String S = " RDLLDDLDU " ; int R = 5 ; int N = S . length ( ) ; System . out . println ( isPossible ( S , R , N ) ) ; } }