Easiest Java Solution [ 100% ] [ 0ms ]
Approach\n1. Initialize variables:\n   - Initialize pro to 1 (to store the product of digits).\n   - Initialize sum to 0 (to store the sum of digits).\n\n2. Ite
# Approach\n1. **Initialize variables:**\n   - Initialize `pro` to 1 (to store the product of digits).\n   - Initialize `sum` to 0 (to store the sum of digits).\n\n2. **Iterate through each digit of `n`:**\n   - Use a loop to iterate through each digit by continuously dividing `n` by 10 until `n` becomes zero.\n\n3. **Calculate the digit:**\n   - At each iteration, calculate the remainder (`digit`) by `n % 10`.\n\n4. **Update the product and sum:**\n   - Update `pro` by multiplying it with the digit (`pro *= digit`).\n   - Update `sum` by adding the digit to it (`sum += digit`).\n\n5. **Return the difference:**\n   - Return the difference between the product and the sum (`pro - sum`).\n\n# Complexity\n- Time complexity:\nLet d be the number of digits in n.\nO(d)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int subtractProductAndSum(int n) {\n        int pro=1, sum=0;\n        while(n>0){\n            int rem=n%10;\n            n=n/10;\n            pro*=rem;\n            sum+=rem;\n        }\n        return pro-sum;\n    }\n}\n```\n\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/0b562660-4188-481b-b33a-5a4d5ee9b9b7_1718604386.6828487.jpeg)\n
['Java']