Count numbers which can be constructed using two numbers | Returns count of numbers from 1 to n that can be formed using x and y . ; Create an auxiliary array and initialize it as false . An entry arr [ i ] = true is going to mean that i can be formed using x and y ; x and y can be formed using x and y . ; Initialize result ; Traverse all numbers and increment result if a number can be formed using x and y . ; If i can be formed using x and y ; Then i + x and i + y can also be formed using x and y . ; Increment result ; Driver code
< ? php function countNums ( $ n , $ x , $ y ) { $ arr = array_fill ( 0 , $ n + 1 , false ) ; if ( $ x <= $ n ) $ arr [ $ x ] = true ; if ( $ y <= $ n ) $ arr [ $ y ] = true ; $ result = 0 ; for ( $ i = min ( $ x , $ y ) ; $ i <= $ n ; $ i ++ ) { if ( $ arr [ $ i ] ) { if ( $ i + $ x <= $ n ) $ arr [ $ i + $ x ] = true ; if ( $ i + $ y <= $ n ) $ arr [ $ i + $ y ] = true ; $ result ++ ; } } return $ result ; } $ n = 15 ; $ x = 5 ; $ y = 7 ; echo countNums ( $ n , $ x , $ y ) ; ? >