Skip to content

Practice Subtract the Product and Sum of Digits of an Integer

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1281
LeetCode Problem

Subtract the Product and Sum of Digits of an Integer

Problem Statement

For the positive integer n, multiply its decimal digits together and subtract the sum of those digits. Return the difference in that order: product minus sum. Practice approach: Repeatedly take n modulo 10 to extract a digit, then use integer division by 10 to remove it. Start the product at one and the sum at zero. A zero digit makes the product zero but still contributes zero to the sum. Processing d digits uses O(d) time and O(1) extra space. Test a single digit and a number containing zero.

Source: Subtract the Product and Sum of Digits of an Integer on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 234
Output
15

# Example 2

Input
n = 4421
Output
21

Constraints

1 <= n <= 10^5

Related LeetCode Problems

Practice more problems similar to Subtract the Product and Sum of Digits of an Integer.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.