Problem
Given a positive integer, multiply all its digits together and add all its digits separately, then return the product minus the sum. The challenge is extracting each digit efficiently without relying on string conversion.
- Input: n = 234
- Output: 15
- Explanation: Digits are 2, 3, 4; product 2ร3ร4 = 24, sum 2+3+4 = 9, so 24 โ 9 = 15.
A second example: n = 4421 โ product 4ร4ร2ร1 = 32, sum 4+4+2+1 = 11, result 32 โ 11 = 21.
Intuition
The modulo operation (n % 10) yields the rightmost digit, and integer division (n // 10) removes it. Repeating this peels off every digit from right to left in a single loop, so both the product and the sum can be computed together in one pass without converting the number to a string.
Solution โ Digit Extraction
Initialize the product to 1 and the sum to 0, then peel digits off the right end of the number one at a time, updating both accumulators on each iteration.
- Set
digit_product = 1anddigit_sum = 0. - While
n > 0, extract the last digit:digit = n % 10. - Multiply
digitintodigit_productand add it todigit_sum. - Remove the last digit:
n //= 10. - Return
digit_product - digit_sum.
1class Solution:
2 def subtractProductAndSum(self, n: int) -> int:
3 digit_product = 1
4 digit_sum = 0
5
6 while n > 0:
7 digit = n % 10 # rightmost digit of what remains
8 digit_product *= digit
9 digit_sum += digit
10 n //= 10 # drop the rightmost digit
11
12 return digit_product - digit_sumTime: O(log n) โ the loop runs once per digit, and n has โlogโโ(n)โ + 1 digits.
Space: O(1) โ only two accumulator variables regardless of how large n is.
Complexity Summary
| Approach | Time | Space | When to use |
|---|---|---|---|
| Digit Extraction | O(log n) | O(1) | Any single-pass problem that computes something over each decimal digit |
Common Mistakes
- Initializing
digit_product = 0instead of1: Multiplying 0 by anything stays 0 โ the multiplicative identity is 1, not 0. - Returning
digit_sum - digit_productinstead ofdigit_product - digit_sum: The problem specifically asks for product minus sum; swapping the order gives the negated result. - Using string conversion but forgetting to cast back to int:
for d in str(n)yields characters, so arithmetic requiresint(d)โ'2' * '3'in Python repeats the string three times rather than multiplying the integers. - Forgetting to update n inside the loop: Writing
digit = n % 10without the follow-upn //= 10extracts the same last digit on every iteration and loops forever. - Adding a special case for single-digit inputs: The loop handles n = 1 through 9 correctly in one iteration โ no guard is needed.
Related Problems
happy-numberโ repeatedly extracts digits, squares them, and sums them using the same modulo-and-divide loop to test for a cyclereverse-integerโ peels digits with% 10and reconstructs the number in reverse order with* 10palindrome-numberโ uses digit extraction to build the reversed half of the number for comparisonfind-numbers-with-even-number-of-digitsโ counts how many digits each number has using the same loop structure