EasyNumber Theory & Math

Subtract the Product and Sum of Digits of an Integer โ€” Solution

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.

  1. Set digit_product = 1 and digit_sum = 0.
  2. While n > 0, extract the last digit: digit = n % 10.
  3. Multiply digit into digit_product and add it to digit_sum.
  4. Remove the last digit: n //= 10.
  5. 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_sum

Time: 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

ApproachTimeSpaceWhen to use
Digit ExtractionO(log n)O(1)Any single-pass problem that computes something over each decimal digit

Common Mistakes

  • Initializing digit_product = 0 instead of 1: Multiplying 0 by anything stays 0 โ€” the multiplicative identity is 1, not 0.
  • Returning digit_sum - digit_product instead of digit_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 requires int(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 % 10 without the follow-up n //= 10 extracts 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 cycle
  • reverse-integer โ€” peels digits with % 10 and reconstructs the number in reverse order with * 10
  • palindrome-number โ€” uses digit extraction to build the reversed half of the number for comparison
  • find-numbers-with-even-number-of-digits โ€” counts how many digits each number has using the same loop structure

Ready to practice? Try it on SkillFlow

Adaptive problems, AI follow-up interviews, and a skill score that shows exactly where you need to improve.

Practice This Problem โ†’