Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver Code
< ? php function digitSum ( $ n ) { $ digSum = 0 ; while ( $ n ) { $ digSum += $ n % 10 ; $ n /= 10 ; } return $ digSum ; } function countInteger ( $ n , $ s ) { if ( $ n < $ s ) return 0 ; for ( $ i = $ s ; $ i <= min ( $ n , $ s + 163 ) ; $ i ++ ) if ( ( $ i - digitSum ( $ i ) ) > $ s ) return ( $ n - $ i + 1 ) ; return 0 ; } $ n = 1000 ; $ s = 100 ; echo countInteger ( $ n , $ s ) ; ? >