Check if N can be represented as sum of integers chosen from set { A , B } | Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver Code
< ? php function checkIfPossibleRec ( $ x , $ a , $ b , $ isPossible , $ n ) { if ( $ x > $ n ) return ; if ( $ isPossible == true ) return ; $ isPossible [ $ x ] = true ; checkIfPossibleRec ( $ x + $ a , $ a , $ b , $ isPossible , $ n ) ; checkIfPossibleRec ( $ x + $ b , $ a , $ b , $ isPossible , $ n ) ; } function checkPossible ( $ n , $ a , $ b ) { $ isPossible [ $ n + 1 ] = array ( false ) ; checkIfPossibleRec ( 0 , $ a , $ b , $ isPossible , $ n ) ; return $ isPossible ; } $ a = 3 ; $ b = 7 ; $ n = 8 ; if ( checkPossible ( $ a , $ b , $ n ) ) echo " No " ; else echo " Yes " ; ? >