Related Articles
Count of Numbers such that difference between the number and sum of its digits not less than L
• Last Updated : 22 Jan, 2019

Given a Natural number N and a whole number L, the task is to find the count of numbers, smaller than or equal to N, such that the difference between the number and sum of its digits is not less than L.

Examples:

```Input: N = 1500, L = 30
Output: 1461

Input: N = 1546300, L = 30651
Output: 1515631
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
Our solution depends on a simple observation that if a number, say X, is such that the difference of X and sumOfDigits(X) is less than or equal to L, then X+1 is also a valid number. Hence, we have to find a minimum such X using binary search.

Implementation:

## C++

 `// C++ implementation of above approach``#include ``using` `namespace` `std;`` ` `// Function to calculate the``// sum of digits``int` `sumOfDigits(``long` `long` `n)``{``    ``long` `long` `sum = 0;`` ` `    ``while` `(n > 0) {``        ``sum += n % 10;``        ``n /= 10;``    ``}`` ` `    ``return` `sum;``}`` ` `// Function to count the numbers``long` `long` `countDigits(``long` `long` `num, ``long` `long` `limit)``{``    ``long` `long` `left = 1, right = num, result = 0;`` ` `    ``// use binary search``    ``while` `(left <= right) {`` ` `        ``long` `long` `mid = (right + left) / 2;`` ` `        ``// Check if you are at a valid number or not``        ``if` `((mid - sumOfDigits(mid)) >= limit) {`` ` `            ``// update the answer at each valid step``            ``result = num - mid + 1;``            ``right = mid - 1;``        ``}``        ``else` `{`` ` `            ``left = mid + 1;``        ``}``    ``}`` ` `    ``// return the answer``    ``return` `result;``}`` ` `// Driver code``int` `main()``{``    ``// Get the value of N and L``    ``long` `long` `N = 1546300, L = 30651;`` ` `    ``// Count the numbers``    ``cout << countDigits(N, L);`` ` `    ``return` `0;``}`

## Java

 `// Java implementation of above approach`` ` `class` `GFG``{``    ``// Function to calculate the``    ``// sum of digits``    ``static` `long` `sumOfDigits( ``long` `n )``    ``{``        ``long` `sum = ``0``;``     ` `        ``while` `(n > ``0``) ``        ``{``            ``sum += n % ``10``;``            ``n /= ``10``;``        ``}``     ` `        ``return` `sum;``    ``}``     ` `    ``// Function to count the numbers``    ``static` `long` `countDigits(``long` `num, ``long` `limit)``    ``{``        ``long` `left = ``1``, right = num, result = ``0``;``     ` `        ``// use binary search``        ``while` `(left <= right)``        ``{``            ``long` `mid = (right + left) / ``2``;``     ` `            ``// Check if you are at a valid number or not``            ``if` `((mid - sumOfDigits(mid)) >= limit)``            ``{``     ` `                ``// update the answer at each valid step``                ``result = num - mid + ``1``;``                ``right = mid - ``1``;``            ``}``            ``else``            ``{``                ``left = mid + ``1``;``            ``}``        ``}``     ` `        ``// return the answer``        ``return` `result;``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `main(String []args)``    ``{``        ``// Get the value of N and L``        ``long` `N = ``1546300``, L = ``30651``;``     ` `        ``// Count the numbers``        ``System.out.println(countDigits(N, L));``    ``}``}`` ` `// This code is contributed by ihritik`

## Python3

 `# Python3 program for above approach`` ` `# Function to calculate the sum of digits``def` `sumOfDigits(n):``    ``sum` `=` `0`` ` `    ``while` `(n > ``0``):``        ``sum` `+``=` `n ``%` `10``        ``n ``=` `int``(n ``/` `10``)`` ` `    ``return` `sum`` ` `# Function to count the numbers``def` `countDigits(num, limit):``    ``left ``=` `1``    ``right ``=` `num``    ``result ``=` `0`` ` `    ``# use binary search``    ``while` `(left <``=` `right):``        ``mid ``=` `int``((right ``+` `left) ``/` `2``)`` ` `        ``# Check if you are at a valid number or not``        ``if` `((mid ``-` `sumOfDigits(mid)) >``=` `limit):``             ` `            ``# update the answer at each valid step``            ``result ``=` `num ``-` `mid ``+` `1``            ``right ``=` `mid ``-` `1``         ` `        ``else``:``            ``left ``=` `mid ``+` `1``         ` `    ``# return the answer``    ``return` `result`` ` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``     ` `    ``# Get the value of N and L``    ``N ``=` `1546300``    ``L ``=` `30651`` ` `    ``# Count the numbers``    ``print``(countDigits(N, L))`` ` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation of above approach``using` `System;`` ` `class` `GFG``{``    ``// Function to calculate the``    ``// sum of digits``    ``static` `long` `sumOfDigits( ``long` `n )``    ``{``        ``long` `sum = 0;``     ` `        ``while` `(n > 0) ``        ``{``            ``sum += n % 10;``            ``n /= 10;``        ``}``     ` `        ``return` `sum;``    ``}``     ` `    ``// Function to count the numbers``    ``static` `long` `countDigits(``long` `num, ``long` `limit)``    ``{``        ``long` `left = 1, right = num, result = 0;``     ` `        ``// use binary search``        ``while` `(left <= right) {``     ` `            ``long` `mid = (right + left) / 2;``     ` `            ``// Check if you are at a valid number or not``            ``if` `((mid - sumOfDigits(mid)) >= limit) ``            ``{``     ` `                ``// update the answer at each valid step``                ``result = num - mid + 1;``                ``right = mid - 1;``            ``}``            ``else` `            ``{``     ` `                ``left = mid + 1;``            ``}``        ``}``     ` `        ``// return the answer``        ``return` `result;``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``// Get the value of N and L``        ``long` `N = 1546300, L = 30651;``     ` `        ``// Count the numbers``        ``Console.WriteLine(countDigits(N, L));``    ``}``}`` ` `// This code is contributed by ihritik`

## PHP

 ` 0)``    ``{ ``        ``\$sum` `+= ``\$n` `% 10; ``        ``\$n` `/= 10; ``    ``} `` ` `    ``return` `\$sum``; ``} `` ` `// Function to count the numbers ``function` `countDigits(``\$num``, ``\$limit``) ``{ ``    ``\$left` `= 1;``    ``\$right` `= ``\$num``;``    ``\$result` `= 0; `` ` `    ``// use binary search ``    ``while` `(``\$left` `<= ``\$right``) ``    ``{ `` ` `        ``\$mid` `= ``floor``((``\$right` `+ ``\$left``) / 2); `` ` `        ``// Check if you are at a valid number or not ``        ``if` `((``\$mid` `- sumOfDigits(``\$mid``)) >= ``\$limit``) ``        ``{ `` ` `            ``// update the answer at each valid step ``            ``\$result` `= ``\$num` `- ``\$mid` `+ 1; ``            ``\$right` `= ``\$mid` `- 1; ``        ``} ``        ``else` `        ``{ `` ` `            ``\$left` `= ``\$mid` `+ 1; ``        ``} ``    ``} `` ` `    ``// return the answer ``    ``return` `\$result``; ``} `` ` `// Driver code `` ` `// Get the value of N and L ``\$N` `= 1546300;``\$L` `= 30651; `` ` `// Count the numbers ``echo` `countDigits(``\$N``, ``\$L``); `` ` `// This code is contributed by Ryuga``?>`
Output:
```1515631
```

Time Complexity: O(log N)
Auxiliary Space: O(1)

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up