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^5Problem 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.
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.
Time Complexity: O(d), where d is the number of digits in n.
Space Complexity: O(1), since no significant extra space is used.
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.
Time Complexity: O(d), where d is the number of digits.
Space Complexity: O(d), due to string storage.
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).
| Approach | Complexity |
|---|---|
| Loop through Digits Approach | Time Complexity: O(d), where d is the number of digits in n. |
| String Conversion Approach | Time Complexity: O(d), where d is the number of digits. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Loop Through Digits (Math) | O(d) | O(1) | Best general solution; minimal memory and standard interview expectation |
| String Conversion | O(d) | O(d) | Useful when already processing digits as strings or prioritizing readability |
LeetCode 1281: Subtract the Product and Sum of Digits of Integer - Interview Prep Ep 25 • Fisher Coder • 3,492 views views
Watch 9 more video solutions →Practice Subtract the Product and Sum of Digits of an Integer with our built-in code editor and test cases.
Practice on FleetCode