Skip to main content

Subtract the Product and Sum of Digits of an Integer - Solution & Explanation

EasyMath14 min readAsked at: Amazon, Meta, Google +3
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given an integer n. Compute the product of its digits and the sum of its digits, then return product - sum. The task mainly tests basic math operations and how you extract digits from a number.

Approach 1: Loop Through Digits (O(d) time, O(1) space)

This is the standard mathematical approach. Repeatedly extract the last digit using n % 10, update both the running product and running sum, and then remove the digit with integer division n // 10. Continue until the number becomes zero. Each iteration processes one digit, so the total work is proportional to the number of digits d.

The key idea is that modulus gives the current digit and division shifts the number right by one decimal place. This avoids converting the number to another data type and keeps memory usage constant. The algorithm performs simple arithmetic operations while iterating through digits, making it both efficient and easy to reason about. Time complexity is O(d) and space complexity is O(1).

This pattern appears frequently in math and simulation problems where you need to process digits individually.

Approach 2: String Conversion (O(d) time, O(d) space)

Another option converts the integer into a string and iterates over its characters. For each character, convert it back to a digit and update the product and sum variables. This method removes the need for modulus and division operations, which some developers find easier to read.

The logic becomes straightforward: iterate through the string, compute digit = ch - '0' (or equivalent conversion), multiply it into the product, and add it to the sum. Because the string representation stores all digits, this approach requires O(d) additional space. Time complexity remains O(d) since every digit is processed once.

This method is often used when working with string manipulation or when consistency with other string-based operations matters.

Recommended for interviews: The loop-through-digits math approach is what interviewers usually expect. It demonstrates comfort with digit extraction using modulus and division, a common pattern in integer manipulation problems. The string approach still works and is readable, but it adds unnecessary space usage. Showing the math-based method signals stronger problem-solving fundamentals.

Approach 1: Loop through Digits Approach

This approach involves extracting each digit by using modulus and division operations. We'll calculate the product and sum of digits by iterating through the digits.

The loop continues extracting the last digit using modulus operation. Each extracted digit is used to update the product and sum, and then removed through division. This process is repeated until all digits are processed.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(d), where d is the number of digits in n.
Space Complexity: O(1), since no significant extra space is used.

Try this approach in the editor →

Approach 2: String Conversion Approach

Convert the number to a string, then iterate through each character, parsing it back to an integer to compute the product and sum of the digits.

Convert the integer to a string to easily access each digit, converting back to integer for calculation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(d), where d is the number of digits.
Space Complexity: O(d), due to string storage.

Try this approach in the editor →

Approach 3: Simulation

We use two variables x and y to record the product of the digits and the sum of the digits respectively. At the beginning, x=1,y=0.

When n \gt 0, each time we take the mod of n by 10 to get the current digit v, and continue the next loop by dividing n by 10. In each loop, we update x = x times v, y = y + v.

Finally, we return x - y.

The time complexity is O(log n), where n is the given integer. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C#

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Loop through Digits Approach

Time Complexity: O(d), where d is the number of digits in n.
Space Complexity: O(1), since no significant extra space is used.

String Conversion Approach

Time Complexity: O(d), where d is the number of digits.
Space Complexity: O(d), due to string storage.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Loop Through Digits (Math)O(d)O(1)Best general solution; minimal memory and standard interview expectation
String ConversionO(d)O(d)Useful when already processing digits as strings or prioritizing readability

Video Solution

LeetCode 1281: Subtract the Product and Sum of Digits of Integer - Interview Prep Ep 25 • Fisher Coder • 3,500 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Subtract the Product and Sum of Digits of an Integer easy or hard?
Subtract the Product and Sum of Digits of an Integer is classified as an Easy problem. It mainly tests basic math operations, loops, and digit extraction. The algorithm is short and typically solved in O(d) time with constant space.
Subtract the Product and Sum of Digits of an Integer Python/Java solution
In Python or Java, iterate through the digits using modulus and division or convert the integer to a string and loop through characters. Maintain two variables: one for the product and one for the sum. After processing all digits, return product - sum.
How to solve Subtract the Product and Sum of Digits of an Integer in O(n)?
Treat n as a number with d digits and iterate through it using modulus and division. At every step, extract the last digit, multiply it into the product, add it to the sum, and remove it from the number. After processing all digits, return product minus sum. This runs in O(d) time and constant space.
What is the best approach for Subtract the Product and Sum of Digits of an Integer?
The most efficient approach iterates through the digits using modulus and division operations. Extract each digit with n % 10, update the running product and sum, and reduce the number using n // 10. This processes each digit exactly once with O(d) time and O(1) space complexity.
Is Subtract the Product and Sum of Digits of an Integer asked at Google/Amazon/Meta?
This problem is categorized as an easy-level math problem and is more common in practice platforms or early interview rounds. Large companies like Google or Amazon typically use it to test basic programming fundamentals such as loops, integer manipulation, and arithmetic operations.
What data structure is used in Subtract the Product and Sum of Digits of an Integer?
No advanced data structures are required. The optimal solution relies on simple integer arithmetic to extract digits. Some implementations optionally convert the number to a string, but the core logic only needs basic variables for the running product and sum.
What is the time complexity of Subtract the Product and Sum of Digits of an Integer?
The time complexity is O(d), where d is the number of digits in the integer. Each digit is processed once while calculating the running product and sum. Space complexity is O(1) when using the mathematical digit extraction approach.

Ready to solve this problem?

Practice Subtract the Product and Sum of Digits of an Integer with our built-in code editor and test cases.

Practice on FleetCode