Subtract the Product and Sum of Digits of an Integer

Problem Id: 1281 Difficulty: Easy Tag: Math


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


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)