Subtract the Product and Sum of Digits of an Integer

Problem Id: 1281 Difficulty: Easy Tag: Math


Intuition

This is a simple problem. We just need to calculate sum and product of each digits in the given number.

Solution


class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        digits = [int(c) for c in str(n)]
        product = 1
        for d in digits:
            product *= d
        return product - sum(digits)