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
function isPossible ( S , R , N ) { var cntl = 0 , cntr = 0 ; var cntu = 0 , cntd = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) cntl ++ ; else if ( S [ i ] == ' ' ) cntr ++ ; else if ( S [ i ] == ' ' ) cntu ++ ; else cntd ++ ; } if ( Math . max ( Math . max ( cntl , cntr ) , Math . max ( cntu , cntd ) ) >= R ) return " " ; var mp = new Map ( ) ; var r_square = R * R ; for ( var i = 1 ; i * i <= r_square ; i ++ ) { mp . set ( i * i , i ) ; if ( mp . has ( r_square - i * i ) ) { if ( Math . max ( cntl , cntr ) >= mp . get ( r_square - i * i ) && Math . max ( cntu , cntd ) >= i ) return " " ; if ( Math . max ( cntl , cntr ) >= i && Math . max ( cntu , cntd ) >= mp . get ( r_square - i * i ) ) return " " ; } } return " " ; } var S = " " ; var R = 5 ; var N = S . length ; document . write ( isPossible ( S , R , N ) ) ;