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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find two numbers made up of a given digit such that their difference is divisible by N
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count numbers with difference between number and its digit sum greater than specific value
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of Numbers in Range where first digit is equal to last digit of the number
- 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
- Sum of all N digit palindrome numbers
- Count numbers from 1 to n that have 4 as a digit
- XOR and OR of all N-digit Armstrong numbers
- Composite numbers with digit sum 1
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1
- Find two numbers with sum N such that neither of them contains digit K
- Largest Even and Odd N-digit numbers
- Count numbers having 0 as a digit
- Count numbers having 0 as a digit
- Number of n digit numbers that do not contain 9
- Find Last Digit of a^b for Large Numbers
- Count of N-digit Palindrome numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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