1281. Subtract the Product and Sum of Digits of an Integer LeetCode Solution
In this guide, you will get 1281. Subtract the Product and Sum of Digits of an Integer LeetCode Solution with the best time and space complexity. The solution to Subtract the Product and Sum of Digits of an Integer problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Subtract the Product and Sum of Digits of an Integer solution in C++
- Subtract the Product and Sum of Digits of an Integer solution in Java
- Subtract the Product and Sum of Digits of an Integer solution in Python
- Additional Resources

Problem Statement of Subtract the Product and Sum of Digits of an Integer
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
Complexity Analysis
- Time Complexity:
- Space Complexity:
1281. Subtract the Product and Sum of Digits of an Integer LeetCode Solution in C++
class Solution {
public:
int subtractProductAndSum(int n) {
int prod = 1;
int summ = 0;
for (; n > 0; n /= 10) {
prod *= n % 10;
summ += n % 10;
}
return prod - summ;
}
};
/* code provided by PROGIEZ */
1281. Subtract the Product and Sum of Digits of an Integer LeetCode Solution in Java
class Solution {
public int subtractProductAndSum(int n) {
int prod = 1;
int summ = 0;
for (; n > 0; n /= 10) {
prod *= n % 10;
summ += n % 10;
}
return prod - summ;
}
}
// code provided by PROGIEZ
1281. Subtract the Product and Sum of Digits of an Integer LeetCode Solution in Python
class Solution:
def subtractProductAndSum(self, n: int) -> int:
prod = 1
summ = 0
while n > 0:
prod *= n % 10
summ += n % 10
n //= 10
return prod - summ
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.