Skip to content

Commit e310345

Browse files
committed
Subtract the Product and Sum of Digits of an Integer
1 parent 98fa149 commit e310345

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
3+
4+
Given an integer number n, return the difference between the product of its digits and
5+
the sum of its digits.
6+
7+
Example 1:
8+
Input: n = 234
9+
Output: 15
10+
Explanation:
11+
Product of digits = 2 * 3 * 4 = 24
12+
Sum of digits = 2 + 3 + 4 = 9
13+
Result = 24 - 9 = 15
14+
15+
Example 2:
16+
Input: n = 4421
17+
Output: 21
18+
Explanation:
19+
Product of digits = 4 * 4 * 2 * 1 = 32
20+
Sum of digits = 4 + 4 + 2 + 1 = 11
21+
Result = 32 - 11 = 21
22+
23+
Constraints:
24+
1 <= n <= 10^5
25+
"""
26+
class Solution:
27+
def subtractProductAndSum(self, n: int) -> int:
28+
sum, product = 0, 1
29+
while n:
30+
digit = n % 10
31+
sum += digit
32+
product *= digit
33+
n //= 10
34+
return product - sum

0 commit comments

Comments
 (0)