Skip to main content

Command Palette

Search for a command to run...

Lets Code Everyday - Day 9

Updated
โ€ข4 min read
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:

  1. Declare two variables sum and product, 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.

  2. 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.

  3. Inside the loop, extract the last digit of the input number by taking the remainder of n divided by 10. This is stored in the variable rem.

  4. Add rem to sum to keep a running total of the sum of digits.

  5. Multiply rem with product to keep a running total of the product of digits.

  6. Remove the last digit from n by dividing it by 10. This is done by updating n to n/10.

  7. Once all the digits have been extracted, calculate the difference between product and sum. This is done by subtracting sum from product and returning the result.

  8. 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:

  1. Convert the integer n into a string str using the Integer.toString() method. This allows us to iterate over each digit of the input number using a for loop.

  2. Declare two variables sum and product, 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.

  3. Use a for loop to iterate over each character in the str string. This loop processes each digit of the input number.

  4. Inside the loop, convert the current character c to an integer using the Character.getNumericValue() method. This gives us the value of the current digit.

  5. Add the current digit to sum to keep a running total of the sum of digits.

  6. Multiply the current digit with product to keep a running total of the product of digits.

  7. Once all the digits have been processed, calculate the difference between product and sum. This is done by subtracting sum from product and returning the result.

  8. 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.

More from this blog

Learn-->Share-->Learn

31 posts