Count numbers with difference between number and its digit sum greater than specific value | method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver Code
< ? php function sumOfDigit ( $ K ) { $ sod = 0 ; while ( $ K ) { $ sod += $ K % 10 ; $ K /= 10 ; } return $ sod ; } function totalNumbersWithSpecificDifference ( $ N , $ diff ) { $ low = 1 ; $ high = $ N ; while ( $ low <= $ high ) { $ mid = floor ( ( $ low + $ high ) / 2 ) ; if ( $ mid - sumOfDigit ( $ mid ) < $ diff ) $ low = $ mid + 1 ; else $ high = $ mid - 1 ; } return ( $ N - $ high ) ; } $ N = 13 ; $ diff = 2 ; echo totalNumbersWithSpecificDifference ( $ N , $ diff ) ; ? >