Given a positive value N, we need to find the count of numbers smaller than N such that the difference between the number and sum of its digits is greater than or equal to given specific value diff.

Examples:

Input : N = 13, diff = 2 Output : 4 Then 10, 11, 12 and 13 satisfy the given condition shown below, 10 – sumofdigit(10) = 9 >= 2 11 – sumofdigit(11) = 9 >= 2 12 – sumofdigit(12) = 9 >= 2 13 – sumofdigit(13) = 9 >= 2 Whereas no number from 1 to 9 satisfies above equation so final result will be 4

We can solve this problem by observing a fact that for a number k less than N,

if k – sumofdigit(k) >= diff then above equation will be true for (k+1) also because we know that sumofdigit(k+1) is not greater than sumofdigit(k) + 1 so, k + 1 - sumofdigit(k + 1) >= k - sumofdigit(k) but we know that right side of above inequality is greater than diff, so left side will also be greater than diff.

So, finally we can say that if a number k satisfies the difference condition then (k + 1) will also satisfy same equation so our job is to find the smallest number which satisfies the difference condition then all numbers greater than this and up to N will satisfy the condition so our answer will be N – smallest number we found.

We can find the smallest number satisfying this condition using binary search so total time complexity of solution will be O(log N)

## C/C++

`/* C++ program to count total numbers which ` ` ` `have difference with sum of digits greater ` ` ` `than specific value */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility method to get sum of digits of K ` `int` `sumOfDigit(` `int` `K) ` `{ ` ` ` `// loop until K is not zero ` ` ` `int` `sod = 0; ` ` ` `while` `(K) ` ` ` `{ ` ` ` `sod += K % 10; ` ` ` `K /= 10; ` ` ` `} ` ` ` `return` `sod; ` `} ` ` ` `// method returns count of numbers smaller than N, ` `// satisfying difference condition ` `int` `totalNumbersWithSpecificDifference(` `int` `N, ` `int` `diff) ` `{ ` ` ` `int` `low = 1, high = N; ` ` ` ` ` `// binary search while loop ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` `int` `mid = (low + high) / 2; ` ` ` ` ` `/* if difference between number and its sum ` ` ` `of digit is smaller than given difference ` ` ` `then smallest number will be on left side */` ` ` `if` `(mid - sumOfDigit(mid) < diff) ` ` ` `low = mid + 1; ` ` ` ` ` `/* 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 */` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `// return the difference between 'smallest number ` ` ` `// found' and 'N' as result ` ` ` `return` `(N - high); ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 13; ` ` ` `int` `diff = 2; ` ` ` ` ` `cout << totalNumbersWithSpecificDifference(N, diff); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`/* Java program to count total numbers which ` ` ` `have difference with sum of digits greater ` ` ` `than specific value */` ` ` `class` `Test ` `{ ` ` ` `// Utility method to get sum of digits of K ` ` ` `static` `int` `sumOfDigit(` `int` `K) ` ` ` `{ ` ` ` `// loop until K is not zero ` ` ` `int` `sod = ` `0` `; ` ` ` `while` `(K != ` `0` `) ` ` ` `{ ` ` ` `sod += K % ` `10` `; ` ` ` `K /= ` `10` `; ` ` ` `} ` ` ` `return` `sod; ` ` ` `} ` ` ` ` ` `// method returns count of numbers smaller than N, ` ` ` `// satisfying difference condition ` ` ` `static` `int` `totalNumbersWithSpecificDifference(` `int` `N, ` `int` `diff) ` ` ` `{ ` ` ` `int` `low = ` `1` `, high = N; ` ` ` ` ` `// binary search while loop ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` `int` `mid = (low + high) / ` `2` `; ` ` ` ` ` `/* if difference between number and its sum ` ` ` `of digit is smaller than given difference ` ` ` `then smallest number will be on left side */` ` ` `if` `(mid - sumOfDigit(mid) < diff) ` ` ` `low = mid + ` `1` `; ` ` ` ` ` `/* 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 */` ` ` `else` ` ` `high = mid - ` `1` `; ` ` ` `} ` ` ` ` ` `// return the difference between 'smallest number ` ` ` `// found' and 'N' as result ` ` ` `return` `(N - high); ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `13` `; ` ` ` `int` `diff = ` `2` `; ` ` ` ` ` `System.out.println(totalNumbersWithSpecificDifference(N, diff)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to count total numbers which ` `# have difference with sum of digits greater ` `# than specific value ` ` ` ` ` `# Utility method to get sum of digits of K ` `def` `sumOfDigit(K): ` ` ` ` ` `# loop until K is not zero ` ` ` `sod ` `=` `0` ` ` `while` `(K): ` ` ` ` ` `sod ` `=` `sod ` `+` `K ` `%` `10` ` ` `K ` `=` `K ` `/` `/` `10` ` ` ` ` `return` `sod ` ` ` ` ` `# method returns count of ` `# numbers smaller than N, ` `# satisfying difference condition ` `def` `totalNumbersWithSpecificDifference(N,diff): ` ` ` ` ` `low ` `=` `1` ` ` `high ` `=` `N ` ` ` ` ` `# binary search while loop ` ` ` `while` `(low <` `=` `high): ` ` ` ` ` `mid ` `=` `(low ` `+` `high) ` `/` `/` `2` ` ` ` ` `''' if difference between number and its sum ` ` ` `of digit is smaller than given difference ` ` ` `then smallest number will be on left side'''` ` ` `if` `(mid ` `-` `sumOfDigit(mid) < diff): ` ` ` `low ` `=` `mid ` `+` `1` ` ` ` ` `# if difference between number and its sum ` ` ` `# of digit greater than equal to given ` ` ` `# difference then smallest number will be on ` ` ` `# right side ` ` ` `else` `: ` ` ` ` ` `high ` `=` `mid ` `-` `1` ` ` ` ` ` ` `# return the difference between 'smallest number ` ` ` `# found' and 'N' as result ` ` ` `return` `(N ` `-` `high) ` ` ` `# Driver code to test above methods ` `N ` `=` `13` `diff ` `=` `2` ` ` `print` `(totalNumbersWithSpecificDifference(N, diff)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count total numbers ` `// which have difference with sum of ` `// digits greater than specific value ` `using` `System; ` ` ` `class` `Test { ` ` ` ` ` `// Utility method to get sum ` ` ` `// of digits of K ` ` ` `static` `int` `sumOfDigit(` `int` `K) ` ` ` `{ ` ` ` ` ` `// loop until K is not zero ` ` ` `int` `sod = 0; ` ` ` `while` `(K != 0) ` ` ` `{ ` ` ` `sod += K % 10; ` ` ` `K /= 10; ` ` ` `} ` ` ` `return` `sod; ` ` ` `} ` ` ` ` ` `// method returns count of numbers ` ` ` `// smaller than N, satisfying ` ` ` `// difference condition ` ` ` `static` `int` `totalNumbersWithSpecificDifference(` `int` `N, ` ` ` `int` `diff) ` ` ` `{ ` ` ` `int` `low = 1, high = N; ` ` ` ` ` `// binary search while loop ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` `int` `mid = (low + high) / 2; ` ` ` ` ` `// if difference between number and ` ` ` `// its sum of digit is smaller than ` ` ` `// given difference then smallest ` ` ` `// number will be on left side ` ` ` `if` `(mid - sumOfDigit(mid) < diff) ` ` ` `low = mid + 1; ` ` ` ` ` `// 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 ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `// return the difference between ` ` ` `// 'smallest number found' ` ` ` `// and 'N' as result ` ` ` `return` `(N - high); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 13; ` ` ` `int` `diff = 2; ` ` ` ` ` `Console.Write(totalNumbersWithSpecificDifference(N, diff)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count total numbers which ` `// have difference with sum of digits greater ` `// than specific value ` ` ` `// method to get sum of digits of K ` `function` `sumOfDigit(` `$K` `) ` `{ ` ` ` ` ` `// loop until K is not zero ` ` ` `$sod` `= 0; ` ` ` `while` `(` `$K` `) ` ` ` `{ ` ` ` `$sod` `+= ` `$K` `% 10; ` ` ` `$K` `/= 10; ` ` ` `} ` ` ` `return` `$sod` `; ` `} ` ` ` `// method returns count of ` `// numbers smaller than N, ` `// satisfying difference condition ` `function` `totalNumbersWithSpecificDifference(` `$N` `, ` `$diff` `) ` `{ ` ` ` `$low` `= 1; ` `$high` `= ` `$N` `; ` ` ` ` ` `// binary search while loop ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` `$mid` `= ` `floor` `((` `$low` `+ ` `$high` `) / 2); ` ` ` ` ` `/* if difference between number and its sum ` ` ` `of digit is smaller than given difference ` ` ` `then smallest number will be on left side */` ` ` `if` `(` `$mid` `- sumOfDigit(` `$mid` `) < ` `$diff` `) ` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` ` ` `/* 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 */` ` ` `else` ` ` `$high` `= ` `$mid` `- 1; ` ` ` `} ` ` ` ` ` `// return the difference ` ` ` `// between 'smallest number ` ` ` `// found' and 'N' as result ` ` ` `return` `(` `$N` `- ` `$high` `); ` `} ` ` ` `// Driver Code ` `$N` `= 13; ` `$diff` `= 2; ` `echo` `totalNumbersWithSpecificDifference(` `$N` `, ` `$diff` `); ` ` ` `// This code is contributed by nitin mittal ` `?> ` |

*chevron_right*

*filter_none*

Output:

4

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest number less than N with digit sum greater than the digit sum of N
- Smallest subarray of size greater than K with sum greater than a given value
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Numbers such that difference between the number and sum of its digits not less than L
- Count of N-digit numbers having digit XOR as single digit
- Node having maximum number of nodes less than its value in its subtree
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of array elements which are greater than all elements on its left
- Find the remainder when First digit of a number is divided by its Last digit
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Smallest N digit number with none of its digits as its divisor
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count number of substrings with numeric value greater than X
- Sum of all array elements less than X and greater than Y for Q queries
- Count of Numbers in Range where first digit is equal to last digit of the number