Subtract the Product and Sum of Digits of an Integer (LeetCode 1281): Solution and Explanation
Multiply the digits of n together, add them together, and return the product minus the sum. The whole problem is the standard digit-peeling loop with two accumulators.
Intuition
You need every digit of n once. The remainder n % 10 is the last digit, and integer division n // 10 removes it, so repeating those two operations walks the digits from right to left until n is 0.
Each digit feeds two running values. The product starts at 1 because 1 is the identity for multiplication, and the sum starts at 0 for the same reason with addition. Neither operation cares about order, so reading the digits backwards does not matter.
When the loop ends, both totals are complete and the answer is one subtraction.
Approach: Digit peeling with two accumulators
- Set
product = 1andtotal = 0. - While
n > 0, taked = n % 10, multiply it into product, add it to total, and setn = n // 10. - Return
product - total.
Step-by-step example
n = 234
- product = 1, total = 0.
- 234 % 10 = 4: product = 4, total = 4, n = 23.
- 23 % 10 = 3: product = 12, total = 7, n = 2.
- 2 % 10 = 2: product = 24, total = 9, n = 0.
- n is 0, so return 24 - 9 = 15.
Code: Python, Java, C++
1class Solution:
2 def subtractProductAndSum(self, n: int) -> int:
3 product, total = 1, 0
4 while n > 0:
5 d = n % 10
6 product *= d
7 total += d
8 n //= 10
9 return product - total1class Solution {
2 public int subtractProductAndSum(int n) {
3 int product = 1, total = 0;
4 while (n > 0) {
5 int d = n % 10;
6 product *= d;
7 total += d;
8 n /= 10;
9 }
10 return product - total;
11 }
12}1class Solution {
2public:
3 int subtractProductAndSum(int n) {
4 int product = 1, total = 0;
5 while (n > 0) {
6 int d = n % 10;
7 product *= d;
8 total += d;
9 n /= 10;
10 }
11 return product - total;
12 }
13};Time and space complexity
O(log n)
The loop runs once per decimal digit, which is at most 6 for n up to 10^5.
O(1)
Only the product, the sum and the shrinking n are stored.
Other approaches
Convert to a string (time O(log n), space O(log n))
Turn n into its decimal string and loop over the characters, converting each back to a digit. It reads naturally and is equally fast for numbers this small, but allocates a string.
Common mistakes
- Starting the product at 0. Every multiplication then stays 0.
- Using
/in Python or JavaScript without flooring.234 / 10is 23.4, and the loop never yields clean digits. - Expecting the answer to be non-negative. Any number containing a 0 digit has product 0, so the result is minus the digit sum, for example 705 gives -12.
Frequently asked questions
Does the order of the digits matter?
No. Multiplication and addition are both commutative, so reading the digits from right to left gives the same product and sum as reading them left to right.
Can the product overflow?
No. n has at most 6 digits and 100000 contains zeros, so the largest product is 9^5 = 59049, far inside a 32-bit int.
What happens for a single-digit n?
The product and the sum are both that digit, so the answer is 0.