Given the sum of digitsĀ a and sum of the square of digitsĀ b . Find the smallest number with the given sum of digits and the 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.
Examples:Ā
Ā
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Ā
Approach:Ā
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 )Ā
where 1<=i<=9Ā
Ā
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]Ā
where 1<=i<=9Ā
Ā
If the condition above is met by any of i, reduceĀ a by i andĀ b by i*i and break. Keep on repeating the above process to find all the digits tillĀ a is 0 andĀ b is 0.Ā
Below is the implementation of the above approach:Ā
Ā
C++
// CPP program to find the Smallest number with given sum of // digits and sum of square of digits #include <bits/stdc++.h> using namespace std; Ā
int dp[901][8101]; Ā
// Top down dp to find minimum number of digits with given // sum of digits a and sum of square of digits as b int minimumNumberOfDigits( int a, int b) { Ā Ā Ā Ā // Invalid condition Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā
Ā Ā Ā Ā // Number of digits satisfied Ā Ā Ā Ā if (a == 0 && b == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā
Ā Ā Ā Ā // Memoization Ā Ā Ā Ā if (dp[a][b] != -1) Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b]; Ā
Ā Ā Ā Ā // Initialize ans as maximum as we have to find the Ā Ā Ā Ā // minimum number of digits Ā Ā Ā Ā int ans = 101; Ā
Ā Ā Ā Ā // Check for all possible combinations of digits Ā Ā Ā Ā for ( int i = 9; i >= 1; i--) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // recurrence call Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a - i, b - (i * i)); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā // and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā if (k != -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = min(ans, k + 1); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Returns the minimum number of digits Ā Ā Ā Ā return dp[a][b] = ans; } Ā
// Function to print the digits that gives // sum as a and sum of square of digits as b void printSmallestNumber( int a, int b) { Ā
Ā Ā Ā Ā // initialize the dp array as -1 Ā Ā Ā Ā memset (dp, -1, sizeof (dp)); Ā
Ā Ā Ā Ā // base condition Ā Ā Ā Ā dp[0][0] = 0; Ā
Ā Ā Ā Ā // function call to get the minimum number of digits Ā Ā Ā Ā int k = minimumNumberOfDigits(a, b); Ā
Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā if (k == -1 || k > 100) Ā Ā Ā Ā Ā Ā Ā Ā cout << "-1" ; Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant Ā Ā Ā Ā Ā Ā Ā Ā // digit Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= 9; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= i && b >= i * i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && 1 + dp[a - i][b - i * i] == dp[a][b]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= i * i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver Code int main() { Ā Ā Ā Ā int a = 18, b = 162; Ā Ā Ā Ā // Function call to print the smallest number Ā Ā Ā Ā printSmallestNumber(a, b); } |
C
// C program to find the Smallest number with given sum of // digits and sum of square of digits #include <stdio.h> #include <string.h> Ā
// Find minimum between two numbers. int min( int num1, int num2) { Ā Ā Ā Ā return (num1 > num2) ? num2 : num1; } Ā
int dp[901][8101]; Ā
// Top down dp to find minimum number of digits with given // sum of digits a and sum of square of digits as b int minimumNumberOfDigits( int a, int b) { Ā Ā Ā Ā // Invalid condition Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā
Ā Ā Ā Ā // Number of digits satisfied Ā Ā Ā Ā if (a == 0 && b == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā
Ā Ā Ā Ā // Memoization Ā Ā Ā Ā if (dp[a][b] != -1) Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b]; Ā
Ā Ā Ā Ā // Initialize ans as maximum as we have to find the Ā Ā Ā Ā // minimum number of digits Ā Ā Ā Ā int ans = 101; Ā
Ā Ā Ā Ā // Check for all possible combinations of digits Ā Ā Ā Ā for ( int i = 9; i >= 1; i--) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // recurrence call Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a - i, b - (i * i)); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā // and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā if (k != -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = min(ans, k + 1); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Returns the minimum number of digits Ā Ā Ā Ā return dp[a][b] = ans; } Ā
// Function to print the digits that gives // sum as a and sum of square of digits as b void printSmallestNumber( int a, int b) { Ā Ā Ā Ā // initialize the dp array as -1 Ā Ā Ā Ā memset (dp, -1, sizeof (dp)); Ā
Ā Ā Ā Ā // base condition Ā Ā Ā Ā dp[0][0] = 0; Ā
Ā Ā Ā Ā // function call to get the minimum number of digits Ā Ā Ā Ā int k = minimumNumberOfDigits(a, b); Ā
Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā if (k == -1 || k > 100) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "-1" ); Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant Ā Ā Ā Ā Ā Ā Ā Ā // digit Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= 9; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= i && b >= i * i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && 1 + dp[a - i][b - i * i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā == dp[a][b]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "%d" , i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= i * i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver Code int main() { Ā Ā Ā Ā int a = 18, b = 162; Ā Ā Ā Ā // Function call to print the smallest number Ā Ā Ā Ā printSmallestNumber(a, b); } Ā
// This code is contributed by Sania Kumari Gupta |
Java
import java.util.Arrays; Ā
// Java program to find the Smallest number // with given sum of digits and // sum of square of digits class GFG { Ā
Ā Ā Ā Ā static int dp[][] = new int [ 901 ][ 8101 ]; Ā
// Top down dp to find minimum number of digits with // given sum of digits a and sum of square of digits as b Ā Ā Ā Ā static int minimumNumberOfDigits( int a, int b) { Ā Ā Ā Ā Ā Ā Ā Ā // Invalid condition Ā Ā Ā Ā Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return - 1 ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Number of digits satisfied Ā Ā Ā Ā Ā Ā Ā Ā if (a == 0 && b == 0 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Memoization Ā Ā Ā Ā Ā Ā Ā Ā if (dp[a][b] != - 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize ans as maximum as we have to find theĀ Ā Ā Ā Ā Ā Ā Ā Ā // minimum number of digits Ā Ā Ā Ā Ā Ā Ā Ā int ans = 101 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Check for all possible combinations of digits Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 9 ; i >= 1 ; i--) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recurrence call Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a - i, b - (i * i)); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k != - 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.min(ans, k + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Returns the minimum number of digits Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b] = ans; Ā Ā Ā Ā } Ā
// Function to print the digits that gives // sum as a and sum of square of digits as b Ā Ā Ā Ā static void printSmallestNumber( int a, int b) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // initialize the dp array as -1 Ā Ā Ā Ā Ā Ā Ā Ā for ( int [] row : dp) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Arrays.fill(row, - 1 ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // base condition Ā Ā Ā Ā Ā Ā Ā Ā dp[ 0 ][ 0 ] = 0 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // function call to get the minimum number of digitsĀ Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a, b); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā Ā Ā Ā Ā if (k == - 1 || k > 100 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "-1" ); Ā Ā Ā Ā Ā Ā Ā Ā } else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0 ) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1 ; i <= 9 ; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= i && b >= i * i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && 1 + dp[a - i][b - i * i] == dp[a][b]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= i * i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
// Driver Code Ā Ā Ā Ā public static void main(String args[]) { Ā Ā Ā Ā Ā Ā Ā Ā int a = 18 , b = 162 ; Ā Ā Ā Ā Ā Ā Ā Ā // Function call to print the smallest number Ā Ā Ā Ā Ā Ā Ā Ā printSmallestNumber(a, b); Ā Ā Ā Ā } } Ā
// This code is contributed by PrinciRaj19992 |
Python3
# Python3 program to find the Smallest number # with given sum of digits and # sum of square of digits Ā
dp = [[ - 1 for i in range ( 8101 )] for i in range ( 901 )] Ā
# Top down dp to find minimum number of digits with # given sum of digits a and sum of square of digits as b def minimumNumberOfDigits(a,b): Ā Ā Ā Ā # Invalid condition Ā Ā Ā Ā if (a > b or a < 0 or b < 0 or a > 900 or b > 8100 ): Ā Ā Ā Ā Ā Ā Ā Ā return - 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Number of digits satisfied Ā Ā Ā Ā if (a = = 0 and b = = 0 ): Ā Ā Ā Ā Ā Ā Ā Ā return 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Memoization Ā Ā Ā Ā if (dp[a][b] ! = - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Initialize ans as maximum as we have to find the Ā Ā Ā Ā # minimum number of digits Ā Ā Ā Ā ans = 101 Ā Ā Ā Ā Ā Ā Ā Ā Ā #Check for all possible combinations of digits Ā Ā Ā Ā for i in range ( 9 , 0 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # recurrence call Ā Ā Ā Ā Ā Ā Ā Ā k = minimumNumberOfDigits(a - i, b - (i * i)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā # and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā if (k ! = - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = min (ans, k + 1 ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Returns the minimum number of digits Ā Ā Ā Ā dp[a][b] = ans Ā Ā Ā Ā return ans Ā
# Function to print the digits that gives # sum as a and sum of square of digits as b def printSmallestNumber(a,b): Ā Ā Ā Ā # initialize the dp array as Ā Ā Ā Ā for i in range ( 901 ): Ā Ā Ā Ā Ā Ā Ā Ā for j in range ( 8101 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = - 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # base condition Ā Ā Ā Ā dp[ 0 ][ 0 ] = 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā # function call to get the minimum number of digits Ā Ā Ā Ā k = minimumNumberOfDigits(a, b) Ā Ā Ā Ā Ā Ā Ā Ā Ā # When there does not exists any number Ā Ā Ā Ā if (k = = - 1 or k > 100 ): Ā Ā Ā Ā Ā Ā Ā Ā print ( - 1 ,end = '') Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā # Printing the digits from the most significant digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 and b > 0 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for i in range ( 1 , 10 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā #checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a > = i and b > = i * i and Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1 + dp[a - i][b - i * i] = = dp[a][b]): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print (i,end = '') Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a - = i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b - = i * i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break # Driver Code if __name__ = = '__main__' : Ā Ā Ā Ā a = 18 Ā Ā Ā Ā b = 162 # Function call to print the smallest number Ā Ā Ā Ā printSmallestNumber(a,b) Ā Ā Ā Ā Ā # This code is contributed by sahilshelangia |
C#
// C# program to find the Smallest number // with given sum of digits and // sum of square of digits using System; public class GFG { Ā Ā Ā Ā Ā Ā static int [,]dp = new int [901,8101]; Ā Ā // Top down dp to find minimum number of digits with // given sum of digits a and sum of square of digits as b Ā Ā Ā Ā static int minimumNumberOfDigits( int a, int b) { Ā Ā Ā Ā Ā Ā Ā Ā // Invalid condition Ā Ā Ā Ā Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Number of digits satisfied Ā Ā Ā Ā Ā Ā Ā Ā if (a == 0 && b == 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Memoization Ā Ā Ā Ā Ā Ā Ā Ā if (dp[a,b] != -1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[a,b]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize ans as maximum as we have to find theĀ Ā Ā Ā Ā Ā Ā Ā Ā // minimum number of digits Ā Ā Ā Ā Ā Ā Ā Ā int ans = 101; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Check for all possible combinations of digits Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 9; i >= 1; i--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recurrence call Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a - i, b - (i * i)); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k != -1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.Min(ans, k + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Returns the minimum number of digits Ā Ā Ā Ā Ā Ā Ā Ā return dp[a,b] = ans; Ā Ā Ā Ā } Ā Ā // Function to print the digits that gives // sum as a and sum of square of digits as b Ā Ā Ā Ā static void printSmallestNumber( int a, int b) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // initialize the dp array as -1 Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < dp.GetLength(0); i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < dp.GetLength(1); j++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i, j] = -1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // base condition Ā Ā Ā Ā Ā Ā Ā Ā dp[0,0] = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // function call to get the minimum number of digitsĀ Ā Ā Ā Ā Ā Ā Ā Ā int k = minimumNumberOfDigits(a, b); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā Ā Ā Ā Ā if (k == -1 || k > 100) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine( "-1" ); Ā Ā Ā Ā Ā Ā Ā Ā } else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 1; i <= 9; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= i && b >= i * i Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && 1 + dp[a - i,b - i * i] == dp[a,b]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= i * i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā // Driver Code Ā Ā Ā Ā public static void Main() { Ā Ā Ā Ā Ā Ā Ā Ā int a = 18, b = 162; Ā Ā Ā Ā Ā Ā Ā Ā // Function call to print the smallest number Ā Ā Ā Ā Ā Ā Ā Ā printSmallestNumber(a, b); Ā Ā Ā Ā } } Ā Ā // This code is contributed by PrinciRaj19992 |
Javascript
<script> Ā
// JavaScript program to find the Smallest number // with given sum of digits and // sum of square of digits Ā
Ā Ā // initialize the dp array as -1 Ā dp = new Array(901).fill(-1).map(() => Ā new Array(8101).fill(-1));; Ā
// Top down dp to find minimum // number of digits with // given sum of digits a and // sum of square of digits as b function minimumNumberOfDigits(a, b) { Ā Ā Ā Ā // Invalid condition Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā Ā Ā Ā Ā Ā Ā Ā Ā // Number of digits satisfied Ā Ā Ā Ā if (a == 0 && b == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā // Memoization Ā Ā Ā Ā if (dp[a][b] != -1) Ā Ā Ā Ā Ā Ā Ā Ā return dp[a][b]; Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize ans as maximum as we have to find theĀ Ā Ā Ā Ā // minimum number of digits Ā Ā Ā Ā var ans = 101; Ā Ā Ā Ā Ā Ā Ā Ā Ā // Check for all possible combinations of digits Ā Ā Ā Ā for ( var i = 9; i >= 1; i--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recurrence call Ā Ā Ā Ā Ā Ā Ā Ā var k = minimumNumberOfDigits(a - i, b - (i * i)); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the combination of digits cannot give sum as a Ā Ā Ā Ā Ā Ā Ā Ā // and sum of square of digits as b Ā Ā Ā Ā Ā Ā Ā Ā if (k != -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.min(ans, k + 1); Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā // Returns the minimum number of digits Ā Ā Ā Ā return dp[a][b] = ans; } Ā
// Function to print the digits that gives // sum as a and sum of square of digits as b function printSmallestNumber(a, b) {Ā Ā Ā Ā Ā Ā // base condition Ā Ā Ā Ā dp[0][0] = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā // function call to get the Ā Ā Ā Ā // minimum number of digitsĀ Ā Ā Ā Ā var k = minimumNumberOfDigits(a, b); Ā Ā Ā Ā Ā Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā if (k == -1 || k > 100) Ā Ā Ā Ā Ā Ā Ā Ā document.write( "-1" ); Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the Ā Ā Ā Ā Ā Ā Ā Ā // most significant digit Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( var i = 1; i <= 9; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= i && b >= i * i && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1 + dp[a - i][b - i * i] == dp[a][b]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write( i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= i * i; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
Ā Ā Ā var a = 18, b = 162; Ā
Ā Ā Ā Ā // Function call to print the smallest number Ā Ā Ā Ā printSmallestNumber(a,b); Ā
// This code is contributed by SoumikMondal Ā
</script> |
99
Time Complexity : O(900*8100*9)Ā
Auxiliary Space : O(900*8100)Ā
Efficient approach : Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memoization(top-down) because memoization method needs extra stack space of recursion calls.
Implementation :
C++
// C++ code for the above approach #include <cstring> #include <iostream> using namespace std; Ā
int dp[901][8101]; Ā
// function to find minimum number of digits with given // sum of digits a and sum of square of digits as b int minimumNumberOfDigits( int a, int b) { Ā Ā Ā Ā // base case Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā
Ā Ā Ā Ā if (a == 0 && b == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā
Ā Ā Ā Ā // initialize Dp with -1 Ā Ā Ā Ā memset (dp, -1, sizeof (dp)); Ā
Ā Ā Ā Ā // Base Case Ā Ā Ā Ā dp[0][0] = 0; Ā
Ā Ā Ā Ā // iterating over subproblems to get the current Ā Ā Ā Ā // solution of dp from previous computations Ā Ā Ā Ā for ( int i = 0; i <= a; i++) { Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j <= b; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dp[i][j] == -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int digit = 1; digit <= 9; digit++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i + digit <= a Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && j + digit * digit <= b) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int new_a = i + digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int new_b = j + digit * digit; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dp[new_a][new_b] == -1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || dp[new_a][new_b] > dp[i][j] + 1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[new_a][new_b] = dp[i][j] + 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // return answer Ā Ā Ā Ā return dp[a][b]; } Ā
// Function to print the digits that gives // sum as a and sum of square of digits as b void printSmallestNumber( int a, int b) { Ā Ā Ā Ā int k = minimumNumberOfDigits(a, b); Ā Ā Ā Ā // When there does not exists any number Ā Ā Ā Ā if (k == -1 || k > 100) Ā Ā Ā Ā Ā Ā Ā Ā cout << "-1" ; Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant Ā Ā Ā Ā Ā Ā Ā Ā // digit Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int digit = 1; digit <= 9; digit++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking conditions for minimum digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= digit && b >= digit * digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && dp[a][b] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā == dp[a - digit] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [b - digit * digit] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + 1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= digit * digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver Code int main() { Ā Ā Ā Ā int a = 18, b = 162; Ā Ā Ā Ā printSmallestNumber(a, b); Ā Ā Ā Ā return 0; } |
C#
using System; Ā
class Program { Ā Ā Ā Ā static int [, ] dp = new int [901, 8101]; Ā
Ā Ā Ā Ā // Function to find the minimum number of digits with a Ā Ā Ā Ā // given sum of digits a and sum of squares of digits as Ā Ā Ā Ā // b Ā Ā Ā Ā static int MinimumNumberOfDigits( int a, int b) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // Base case Ā Ā Ā Ā Ā Ā Ā Ā if (a > b || a < 0 || b < 0 || a > 900 || b > 8100) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return -1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (a == 0 && b == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize dp with -1 Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < 901; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < 8101; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i, j] = -1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Base Case Ā Ā Ā Ā Ā Ā Ā Ā dp[0, 0] = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Iterating over subproblems to get the current Ā Ā Ā Ā Ā Ā Ā Ā // solution of dp from previous computations Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i <= a; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j <= b; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dp[i, j] == -1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int digit = 1; digit <= 9; digit++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i + digit <= a Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && j + digit * digit <= b) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int newA = i + digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int newB = j + digit * digit; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dp[newA, newB] == -1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || dp[newA, newB] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā > dp[i, j] + 1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[newA, newB] = dp[i, j] + 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the answer Ā Ā Ā Ā Ā Ā Ā Ā return dp[a, b]; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Function to print the digits that give a sum as a and Ā Ā Ā Ā // sum of the square of digits as b Ā Ā Ā Ā static void PrintSmallestNumber( int a, int b) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int k = MinimumNumberOfDigits(a, b); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // When there does not exist any number Ā Ā Ā Ā Ā Ā Ā Ā if (k == -1 || k > 100) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine( "-1" ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Printing the digits from the most significant Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (a > 0 && b > 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Trying all combinations Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int digit = 1; digit <= 9; digit++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking conditions for the minimum Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // digits Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a >= digit && b >= digit * digit Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && dp[a, b] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā == dp[a - digit, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b - digit * digit] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + 1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(digit); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a -= digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b -= digit * digit; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā static void Main( string [] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int a = 18, b = 162; Ā Ā Ā Ā Ā Ā Ā Ā PrintSmallestNumber(a, b); Ā Ā Ā Ā } } |
99
Time Complexity : O(900*8100*9)Ā
Auxiliary Space : O(900*8100)Ā
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!