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.
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Longest Common Subsequence | DP using Memoization
- Total number of decreasing paths in a matrix
- Sum of product of consecutive Binomial Coefficients
- Advanced master theorem for divide and conquer recurrences
- Count even length binary sequences with same sum of first and second half bits
- Count of n digit numbers whose sum of digits equals to given sum
- Compute sum of digits in all numbers from 1 to n
- Maximum profit by buying and selling a share at most twice
- 0-1 Knapsack Problem | DP-10
- Longest Common Subsequence | DP-4
- Write a program to print all permutations of a given string
- Java Program for Phone Mnemonics
- Rat in a Maze | Backtracking using Stack
- Samsung Semiconductor Institute of Research(SSIR Software) Intern/FTE | Set-2
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