You are given two positive integer value n and s. You have to find the total number of such integer from 1 to n such that the difference of integer and its digit sum is greater than given s.
Input : n = 20, s = 5 Output :11 Explanation : Integer from 1 to 9 have diff(integer - digitSum) = 0 but for 10 to 20 they have diff(value - digitSum) > 5 Input : n = 20, s = 20 Output : 0 Explanation : Integer from 1 to 20 have diff (integer - digitSum) > 5
The very first and basic approach to solve this question is to check for all integer starting from 1 to n and for each check whether integer minus digit sum is greater than s or not. This will become very time costly because we have to traverse 1 to n and for each integer we also have to calculate the digit sum.
Before moving to better approach lets have some key analysis about this questions and its features:
- For the largest possible integer (say long long int i.e. 10^18), the maximum possible digit sum is 9*18 (when all of digits are nine) = 162. This means in any case all the integer greater than s + 162 satisfy the condition of integer – digitSum > s.
- All integer less than s can not satisfy the given condition for sure.
- All the integers within a tens range (0-9, 10-19…100-109) does have same value of integer minus digitSum.
Using above three key features we can shorten our approach and time complexity in a manner where we have to iterate only over s to s+163 integers. Beside checking for all integer within range we only check for each 10th integer (e.g 150, 160, 170..).
// if n < s then return 0 if n<s return 0 else // iterate for s to min(n, s+163) for i=s to i min(n, s+163) // return n-i+1 if (i-digitSum)>s return (n-i+1) // if no such integer found return 0 return 0
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.
- Count of N-digit numbers having digit XOR as single digit
- Largest number less than N with digit sum greater than the digit sum of N
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count n digit numbers not having a particular digit
- Count of divisors having more set bits than quotient on dividing N
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers with difference between number and its digit sum greater than specific value
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Check if frequency of each digit is less than the digit
- Finding LCM of more than two (or array) numbers without using GCD
- Count of Numbers in Range where the number does not contain more than K non zero digits
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- GCD of more than two (or array) numbers
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Generate all N digit numbers having absolute difference as K between adjacent digits
- N digit numbers having difference between the first and last digits as K
- Count three-digit numbers having difference X with its reverse
- Count numbers formed by given two digit with sum having given digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m