Show problems as
Classic problem #1281EasyMath

Subtract the Product and Sum of Digits of an Integer

Real-world version: Crate Serial Checksum
Tracked in this browser only
Read Subtract the Product and Sum of Digits of an Integer on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Every crate leaving the yard is stamped with a serial number serial, a positive whole number written in base ten with no leading zeros.

The yard's clerk works out a checksum for the crate by multiplying all the decimal digits of serial together, adding all those same digits up, and then subtracting the second tally from the first.

Return the checksum. It may be negative.

Examples

Example 1

Input
serial = 705
Output
-12

The digits are 7, 0 and 5. Their product is 0 and their sum is 12, so the checksum is 0 - 12.

Example 2

Input
serial = 48
Output
20

The digits are 4 and 8. Their product is 32 and their sum is 12, so the checksum is 32 - 12.

Example 3

Input
serial = 9
Output
0

A one-digit serial has a product of 9 and a sum of 9, so the checksum is 9 - 9.

Constraints

  • 1 <= serial <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def digit_checksum(serial: int) -> int:
Java
public int digitChecksum(int serial)