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
function sumOfDigit ( K ) { let sod = 0 ; while ( K ) { sod += K % 10 ; K /= 10 ; } return sod ; } function totalNumbersWithSpecificDifference ( N , diff ) { let low = 1 ; let high = N ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } let N = 13 ; let diff = 2 ; document . write ( totalNumbersWithSpecificDifference ( N , diff ) ) ;