Given a positive number N. We need to find number(s) such that sum of digits of those numbers to themselves is equal to N. If no such number is possible print -1. Here N
Input : N = 21 Output : X = 15 Explanation : X + its digit sum = 15 + 1 + 5 = 21 Input : N = 5 Output : -1 Input : N = 100000001 Output : X = 99999937 X = 100000000
Method 1 : (Naive Approach)
We have already discussed the approach here. The approach might not work for N as large as .
Method 2 : (Efficient)
It is a fact that for a number X < = 1000000000, the sum of digits never exceeds 100. Using this piece of information, we can iterate over all possibilities in the range 0 to 100 on both the sides of the number and check if the number X is eqaul to N – sum of digits of X. All the possibilities will be covered in this range.
X = 100000000 X = 99999937
The maximum complexity of this approach can be where len is the number of digits in the number max(len) = 9. Thus the complexity can almost be said to be
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.
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Find a number x such that sum of x and its digits is equal to given n.
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest N digit number with none of its digits as its divisor
- Find M-th number whose repeated sum of digits of a number is N
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Find the smallest number whose sum of digits is N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Smallest number with given sum of digits and sum of square of digits
- Given a number N in decimal base, find number of its digits in any base (base b)
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.