Given sum of digits and sum of square of digits . Find the smallest number with given sum of digits and sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.
Input : a = 18, b = 162
Output : 99
Explanation : 99 is the smallest possible number whose sum of digits = 9 + 9 = 18 and sum of squares of digits is 92+92 = 162.
Input : a = 12, b = 9
Output : -1
Since the smallest number can be of 100 digits, it cannot be stored. Hence the first step to solve it will be to find the minimum number of digits which can give us the sum of digits as and sum of the square of digits as . To find the minimum number of digits, we can use Dynamic Programming. DP[a][b] signifies the minimum number of digits in a number whose sum of the digits will be and sum of the square of digits will be . If there does not exist any such number then DP[a][b] will be -1.
Since the number cannot exceed 100 digits, DP array will be of size 101*8101. Iterate for every digit, and try all possible combination of digits which gives us the sum of digits as and sum of the square of digits as . Store the minimum number of digits in DP[a][b] using the below recurrence relation:
DP[a][b] = min( minimumNumberOfDigits(a – i, b – (i * i)) + 1 )
After getting the minimum number of digits, find the digits. To find the digits, check for all combinations and print those digits which satisfies the condition below:
1 + dp[a – i][b – i * i] == dp[a][b]
If the condition above is met by any of i, reduce by i and by i*i and break. Keep on repeating the above process to find all the digits till is 0 and is 0.
Below is the implementation of above approach:
Time Complexity : O(900*8100*9)
Auxiliary Space : O(900*8100)
Note: Time complexity is in terms of numbers as we are trying all possible combinations of digits.
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.
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest number whose sum of digits is square of N
- Smallest number whose square has N digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Sum of the digits of square of the given number which has only 1's as its digits
- Find smallest number with given number of digits and sum of digits
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Find the smallest number whose sum of digits is N
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number containing all possible N length permutations using digits 0 to D
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest number greater than or equal to N using only digits 1 to K
- Index of smallest triangular number with N digits
- Smallest number k such that the product of digits of k is equal to n
- Smallest number greater than Y with sum of digits equal to X
- Smallest N digit number with none of its digits as its divisor
- Minimum digits to remove to make a number Perfect Square
- Find the smallest positive number which can not be represented by given digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
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.