Best O(logn) Solution
Approach\nUsing Math\n\n# Complexity\n- Time complexity:\nO(logn)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    int subtractProductAn
# Approach\nUsing Math\n\n# Complexity\n- Time complexity:\n$$O(logn)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        int prod = 1, sum = 0;\n        while (n > 0) {\n            int digit = n % 10;\n            sum += digit;\n            prod *= digit;\n            n = n / 10;\n        }\n        return (prod - sum);\n    }\n};\n```
['C++']