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 C++ 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.
- 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
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number k such that the product of digits of k is equal to n
- Smallest multiple of a given number made of digits 0 and 9 only
- Minimum digits to remove to make a number Perfect Square
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Find the smallest number whose digits multiply to a given number n
- Minimum number of digits to be removed so that no two consecutive digits are same
- Immediate smallest number after re-arranging the digits of a given number
- Smallest number by rearranging digits of a given number
- Get the kth smallest number using the digits of the given number
- Find the Largest number with given number of digits and sum of digits
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9
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 : sahilshelangia