Lets Code Everyday - Day 9

Hello there ๐, enthusiasts.
Welcome to another day of learning.
Question - 9 :
Subtract the Product and Sum of Digits of an Integer
Example 1:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
Example 2:
Input: n = 4421
Output: 21
Explanation:
Product of digits = 4 * 4 * 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
Result = 32 - 11 = 21
First approach:
class Solution {
public int subtractProductAndSum(int n) {
int sum=0;
int product=1;
while(n>0){
int rem=n%10;
sum=sum+rem;
product=product*rem;
n=n/10;
}
return product-sum;
}
}
Step-by-step explanation:
Declare two variables
sumandproduct, and initialize them to 0 and 1 respectively. These variables will be used to store the sum and product of the digits of the input number.Use a while loop to extract each digit of the input number one by one. The loop continues until all digits have been extracted. The condition for the loop is
n>0.Inside the loop, extract the last digit of the input number by taking the remainder of
ndivided by 10. This is stored in the variablerem.Add
remtosumto keep a running total of the sum of digits.Multiply
remwithproductto keep a running total of the product of digits.Remove the last digit from
nby dividing it by 10. This is done by updatingnton/10.Once all the digits have been extracted, calculate the difference between
productandsum. This is done by subtractingsumfromproductand returning the result.The function returns an integer value, which is the difference between the product and sum of the digits of the input number.
The time complexity of this code is O(logn), where n is the input integer. This is because the loop iterates over each digit in the input number, and the number of digits in the input number is logarithmic in the value of n.
The space complexity is O(1), as the code uses a fixed amount of memory regardless of the size of the input. Specifically, it uses three integer variables (sum, product, and rem) to perform the computation.
Second approach:
class Solution {
public int subtractProductAndSum(int n) {
String str = Integer.toString(n);
int sum = 0;
int product = 1;
for (char c : str.toCharArray()) {
int digit = Character.getNumericValue(c);
sum += digit;
product *= digit;
}
return product - sum;
}
}
Step-by-step explanation:
Convert the integer
ninto a stringstrusing theInteger.toString()method. This allows us to iterate over each digit of the input number using aforloop.Declare two variables
sumandproduct, and initialize them to 0 and 1 respectively. These variables will be used to store the sum and product of the digits of the input number.Use a
forloop to iterate over each character in thestrstring. This loop processes each digit of the input number.Inside the loop, convert the current character
cto an integer using theCharacter.getNumericValue()method. This gives us the value of the current digit.Add the current digit to
sumto keep a running total of the sum of digits.Multiply the current digit with
productto keep a running total of the product of digits.Once all the digits have been processed, calculate the difference between
productandsum. This is done by subtractingsumfromproductand returning the result.The function returns an integer value, which is the difference between the product and the sum of the digits of the input number.
The time complexity of this code is O(log n), where n is the input integer. This is because the loop iterates over each digit in the input number, and the number of digits in the input number is logarithmic in the value of n.
The space complexity is O(log n), as the code converts the input integer into a string, which takes up additional memory space. Specifically, the space used by the string str is proportional to the number of digits in the input number, which is logarithmic in the value of n. In addition, the code uses three integer variables (sum, product, and digit) to perform the computation, which also takes up a constant amount of space.
Conclusion:
In terms of performance, the first approach may be slightly faster than the second approach due to the overhead involved in string conversion
If code readability is a priority, the second approach may be preferred. If performance is a concern and the input size is large, the first approach may be preferred.
Thank you for continuing to read the blog ๐. I hope it is fruitful.




