Skip to content

Commit

Permalink
Merge pull request nirmalnishant645#12 from nirmalnishant645/problem1
Browse files Browse the repository at this point in the history
Math related question
  • Loading branch information
anantkaushik authored Jan 25, 2020
2 parents 8645566 + 02ab986 commit 5f3b17d
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 5f3b17d

Please sign in to comment.