Skip to content
the poor coder

Leetcode - Subtract the Product and Sum of Digits of an Integer Solution

Poor Man
Published
Updated
Reading time
1 min read

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

Example 1:

Text
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:

Text
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

Solution in Python#

Python
class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        product = 1
        sum_total = 0
        for i in str(n):
            product*=int(i)
            sum_total+=int(i)
        return product - sum_total

Keep going

2021

Leetcode - Maximum 69 Number Solution

Given a positive integer num consisting only of digits 6 and 9. Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6). Example 1: Example 2: Example 3: Constraints: 1 <= num…

leetcodeMath1 min read
2021

Leetcode - Self Dividing Numbers Solution

A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not…

leetcodeMath1 min read
ESC