Leetcode 1281. Subtract the Product and Sum of Digits of an Integer

Problem Explanation

Given an integer number n, the task is to return the difference between the product of the digits of n and the sum of the digits of n.

For instance, if the input number n is 234, then:

  • The product of the digits 2, 3, and 4 is 24 (2 * 3 * 4)
  • The sum of the digits 2, 3, and 4 is 9 (2 + 3 + 4)

So, the result would be the difference between the product and the sum, which is 15 (24 - 9).

Solution Approach

We can solve this problem by using basic mathematical operations as follows:

  1. Initialize two variables prod and summ to calculate the product and sum of the digits respectively. prod is initialized with 1 as we will multiply the digits with it and summ is initialized with 0 as we will add the digits to it.

  2. Iterate over each digit of the number n. To retrieve each digit, we take the modulus of n by 10 (n%10) and then divide n by 10 (n/10).

  3. In each iteration, multiply the digit with prod and add the digit to summ.

  4. Finally, return the result as the difference between prod and summ.

Walkthrough Example

Let's take n = 234 as an example:

  • Initial values: prod = 1, summ = 0
  • For the first digit 4: prod = 4, summ = 4
  • For the second digit 3: prod = 12, summ = 7
  • For the third digit 2: prod = 24, summ = 9
  • The result is prod - summ = 15

Code Solutions

Python

1
2python
3class Solution:
4    def subtractProductAndSum(self, n: int) -> int:
5        prod = 1
6        summ = 0
7        while n:
8            n, digit = divmod(n, 10)
9            prod *= digit
10            summ += digit
11        return prod - summ

Java

1
2java
3public class Solution {
4    public int subtractProductAndSum(int n) {
5        int prod = 1;
6        int summ = 0;
7        while (n > 0) {
8            int digit = n % 10;
9            prod *= digit;
10            summ += digit;
11            n /= 10;
12        }
13        return prod - summ;
14    }
15}

JavaScript

1
2javascript
3class Solution {
4    subtractProductAndSum(n) {
5        let prod = 1;
6        let summ = 0;
7        while (n > 0) {
8            let digit = n % 10;
9            prod *= digit;
10            summ += digit;
11            n = Math.floor(n / 10);
12        }
13        return prod - summ;
14    }
15}

C++

1
2cpp
3class Solution {
4public:
5    int subtractProductAndSum(int n) {
6        int prod = 1;
7        int summ = 0;
8        while (n > 0) {
9            int digit = n % 10;
10            prod *= digit;
11            summ += digit;
12            n /= 10;
13        }
14        return prod - summ;
15    }
16};

C#

1
2csharp
3public class Solution {
4    public int SubtractProductAndSum(int n) {
5        int prod = 1;
6        int summ = 0;
7        while (n > 0) {
8            int digit = n % 10;
9            prod *= digit;
10            summ += digit;
11            n /= 10;
12        }
13        return prod - summ;
14    }
15}

Note: The provided code solutions for the problem are straightforward and implement the mathematical operations as per the described approach. You can see that the base logic stays the same across different programming languages, though the syntax and certain operations might differ from one language to another.To understand the code better, let's further break down the steps:

Python

  • A Python integer n is passed to the function subtractProductAndSum.
  • Two variables prod and summ are initialized to store the product and sum of the digits respectively.
  • As long as n is not zero, the while loop continues to execute where n and digit are updated after each operation. divmod returns two values which are the quotient and the remainder when n is divided by 10. This gives us the last digit of the number and the remaining part of the number without the last digit.
  • The digit is multiplied with prod and added to summ in each iteration.
  • The procedure is repeated until n becomes zero.
  • Finally, the subtracted value is returned as the result.

Java

  • An integer n is passed to the method subtractProductAndSum.
  • The logic is the same as explained in the Python solution, but the syntax is different. Java does not have a built-in divmod function, so we use the % operator to get the last digit and the / operator to remove the last digit from n.
  • The returned result is the difference between prod and summ.

JavaScript

  • A number n is passed to the function subtractProductAndSum.
  • Similar to Java, JavaScript also doesn't have a divmod function. To get the digit and update n, we use the % and / operators. Since JavaScript division does not automatically perform integer division, we use Math.floor function to ensure n is an integer after each division.
  • The difference is returned as the result.

C++

  • An int n is passed to the function subtractProductAndSum.
  • The approach is similar to the ones above. The difference lies primarily in the syntax for the C++ language.

C#

  • An integer n is passed to the function subtractProductAndSum.
  • The syntax and procedure are close to Java, except for minor changes in casing and function naming conventions as per C# standards.

This concludes the explanation of the problem and its solution in Python, Java, JavaScript, C++, and C#. These solutions demonstrate how we can solve a problem involving mathematical operations and looping through individual digits in different programming languages.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫