Back to blog
Aug 13, 2024
3 min read

Subtract the Product and Sum of Digits of an Integer

Given an integer n, return the difference between the product of its digits and the sum of its digits.

Difficulty: Easy | Acceptance: 86.60% | Paid: No Topics: Math

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

Examples

Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
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

Constraints

1 <= n <= 10^5

Iterative Digit Extraction

Intuition Extract each digit using modulo and division operations, then compute product and sum separately.

Steps

  • Initialize product to 1 and sum to 0
  • While n is greater than 0, extract the last digit using modulo 10
  • Multiply product by the digit and add digit to sum
  • Remove the last digit by dividing n by 10
  • Return the difference between product and sum
python
class Solution:
    def subtractProductAndSum(self, n: int) -&gt; int:
        product = 1
        sum_digits = 0
        while n &gt; 0:
            digit = n % 10
            product *= digit
            sum_digits += digit
            n //= 10
        return product - sum_digits

Complexity

  • Time: O(log n) - number of digits in n
  • Space: O(1) - only using constant extra space
  • Notes: Most efficient approach with minimal memory usage

String Conversion

Intuition Convert the number to a string to easily iterate over each digit character.

Steps

  • Convert n to a string representation
  • Iterate through each character in the string
  • Convert each character back to an integer
  • Calculate product and sum of all digits
  • Return the difference
python
class Solution:
    def subtractProductAndSum(self, n: int) -&gt; int:
        digits = [int(d) for d in str(n)]
        product = 1
        sum_digits = 0
        for d in digits:
            product *= d
            sum_digits += d
        return product - sum_digits

Complexity

  • Time: O(log n) - number of digits in n
  • Space: O(log n) - string representation of n
  • Notes: More readable but uses extra memory for string conversion

Functional Reduction

Intuition Use functional programming constructs like reduce to compute product and sum in a declarative way.

Steps

  • Convert n to string and split into individual digit characters
  • Map each character to its integer value
  • Use reduce to compute product with initial value 1
  • Use reduce or sum to compute total of all digits
  • Return the difference
python
from functools import reduce
import operator

class Solution:
    def subtractProductAndSum(self, n: int) -&gt; int:
        digits = [int(d) for d in str(n)]
        product = reduce(operator.mul, digits, 1)
        sum_digits = sum(digits)
        return product - sum_digits

Complexity

  • Time: O(log n) - number of digits in n
  • Space: O(log n) - array of digits
  • Notes: Clean functional style but with overhead of array allocation