✅ Beats 100 % || ☑️ Best C++/Java/Python and JavaScript Solution || Complete Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem simply asks to find the product and sum of the digits of the input integer,
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n*The problem simply asks to find the product and sum of the digits of the input integer, and then return the difference between the product and sum.*\n\n---\n\n# Approach : *Using Modulo (%)*\n<!-- Describe your approach to solving the problem. -->\n1. Initialize prod to 1 and sum to 0, which will be used to accumulate the product and sum of digits, respectively.\n\n1. Use a while loop to iterate through the digits of the input integer n. The loop continues as long as n is not equal to zero.\n\n1. In each iteration, extract the last digit of n by taking the modulus of n with 10, and assign it to a variable lastDigit.\n\n1. Multiply prod by lastDigit to accumulate the product of digits, and add lastDigit to sum to accumulate the sum of digits.\n\n1. Divide n by 10 to remove the last digit and move on to the next digit.\n\n1. Once the loop completes, return the difference between prod and sum.\n\n> This approach uses a simple iterative process to extract the digits of n, compute their product and sum, and then return the difference between the two.\n\n# Complexity\n- Time complexity: O(log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n```C++ []\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        int prod = 1;\n        int sum = 0;\n        while(n != 0){\n            int lastDigit = n%10;\n            prod = prod * lastDigit;\n            sum = sum + lastDigit;\n            n = n/10;\n        }\n        return prod - sum;\n    }\n};\n```\n```java []\nclass Solution {\n    public int subtractProductAndSum(int n) {\n        int prod = 1;\n        int sum = 0;\n        while(n != 0){\n            int lastDigit = n%10;\n            prod = prod * lastDigit;\n            sum = sum + lastDigit;\n            n = n/10;\n        }\n        return prod - sum;\n    }\n}\n```\n```Python []\nclass Solution(object):\n    def subtractProductAndSum(self, n):\n        prod = 1\n        sum = 0\n        while n != 0:\n            lastDigit = n % 10\n            prod *= lastDigit\n            sum += lastDigit\n            n = n // 10\n        return prod - sum\n```\n```JavaScript []\nvar subtractProductAndSum = function(n) {\n    let prod = 1;\n    let sum = 0;\n    while(n !== 0){\n        let lastDigit = n % 10;\n        prod *= lastDigit;\n        sum += lastDigit;\n        n = Math.floor(n / 10);\n    }\n    return prod - sum;\n};\n```\n\uD83D\uDE80 Try it out for yourself and see how it performs!\n\n\n---\n\n\n# Approach : *Using String*\n<!-- Describe your approach to solving the problem. -->\n- The optimized solution converts the input integer to a string using the to_string() function. This makes it easy to iterate over each digit of the number using a for loop.\n\n- In the for loop, we iterate over each character in the string and convert it to an integer by subtracting the character \'0\'. This works because in the ASCII table, the character \'0\' has a value of 48, and the characters \'1\' to \'9\' have consecutive values from 49 to 57.\n\n- We then update the product and sum variables by multiplying and adding the current digit to them respectively.\n\n- Finally, we return the difference between the product and sum.\n\n> This approach avoids the expensive modulo and division operations used in the previous solution, which can be slow for large integers.\n\n# Complexity\n- Time complexity: O(d)\n> d -> Number of Digits \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(d)\n> Since here we have used a string of length d\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n```C++ []\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        int prod = 1, sum = 0;\n        string s = to_string(n);\n        for (char c : s) {\n            int digit = c - \'0\';\n            prod *= digit;\n            sum += digit;\n        }\n        return prod - sum;\n    }\n};\n```\n```java []\nclass Solution {\n    public int subtractProductAndSum(int n) {\n        int prod = 1, sum = 0;\n        String s = Integer.toString(n);\n        for (char c : s.toCharArray()) {\n            int digit = c - \'0\';\n            prod *= digit;\n            sum += digit;\n        }\n        return prod - sum;\n    }\n}\n```\n```Python []\nclass Solution(object):\n    def subtractProductAndSum(self, n):\n        prod = 1\n        sum = 0\n        s = str(n)\n        for c in s:\n            digit = int(c)\n            prod *= digit\n            sum += digit\n        return prod - sum\n```\n```JavaScript []\nvar subtractProductAndSum = function(n) {\n    let prod = 1;\n    let sum = 0;\n    let s = n.toString();\n    for (let i = 0; i < s.length; i++) {\n        let digit = parseInt(s[i]);\n        prod *= digit;\n        sum += digit;\n    }\n    return prod - sum;\n};\n```\n\uD83D\uDE80 Try it out for yourself and see how it performs!\n\n---\n\n![upvote.jpeg](https://assets.leetcode.com/users/images/2dca81a7-0747-4f57-8360-eb7c021b3626_1682509277.815069.jpeg)\n\n
['String', 'Python', 'C++', 'Java', 'JavaScript']