Count of Numbers such that difference between the number and sum of its digits not less than L
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
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 <bits/stdc++.h> 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
<?php // PHP implementation of the above approach // Function to calculate the // sum of digits function sumOfDigits( $n ) { $sum = 0; while ( $n > 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 ?> |
Javascript
<script> // JavaScript implementation of above approach // Function to calculate the // sum of digits function sumOfDigits(n) { let sum = 0; while (n > 0) { sum += n % 10; n = parseInt(n / 10, 10); } return sum; } // Function to count the numbers function countDigits(num, limit) { let left = 1, right = num, result = 0; // use binary search while (left <= right) { let mid = parseInt((right + left) / 2, 10); // 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; } // Get the value of N and L let N = 1546300, L = 30651; // Count the numbers document.write(countDigits(N, L)); </script> |
Output:
1515631
Time Complexity: O(log N)
Auxiliary Space: O(1)