Skip to content

Commit

Permalink
Math related question
Browse files Browse the repository at this point in the history
  • Loading branch information
nirmalnishant645 authored Jan 24, 2020
1 parent 2cd537e commit 02ab986
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 1281-Subtract-the-Product-and-Sum-of-Digits-of-an-integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
Given an integer number n, return the difference between the product of its digits and the sum of its digits.
Example 1:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
Example 2:
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
'''
class Solution:
def subtractProductAndSum(self, n: int) -> int:
digit, s, p = 0, 0, 1
while n > 0:
digit = n%10
n = n//10
p = p * digit
s = s + digit
return p - s

0 comments on commit 02ab986

Please sign in to comment.