Check if it is possible to reach any point on the circumference of a given circle from origin | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 [ i ] == ' L ' ) cntl ++ ; else if ( S [ i ] == ' R ' ) cntr ++ ; else if ( S [ i ] == ' U ' ) cntu ++ ; else cntd ++ ; } if ( max ( max ( cntl , cntr ) , max ( cntu , cntd ) ) >= R ) return " Yes " ; unordered_map < int , int > mp ; int r_square = R * R ; for ( int i = 1 ; i * i <= r_square ; i ++ ) { mp [ i * i ] = i ; if ( mp . find ( r_square - i * i ) != mp . end ( ) ) { if ( max ( cntl , cntr ) >= mp [ r_square - i * i ] && max ( cntu , cntd ) >= i ) return " Yes " ; if ( max ( cntl , cntr ) >= i && max ( cntu , cntd ) >= mp [ r_square - i * i ] ) return " Yes " ; } } return " No " ; } int main ( ) { string S = " RDLLDDLDU " ; int R = 5 ; int N = S . length ( ) ; cout << isPossible ( S , R , N ) ; return 0 ; }