C++ Solution with comment and TC
\n\n# Code\n\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        // initialise sum equals to zero and product= 1\n        int sum = 0;\n
\n\n# Code\n```\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        // initialise sum equals to zero and product= 1\n        int sum = 0;\n        int product = 1;\n        \n        // run a loop until the digit is 0\n        while(n>0){\n            // update sum by adding its last digit\n            sum += n%10;\n            // update product by mutiplying its last digit\n            product *= n%10;\n            // update n\n            n /= 10;\n        }\n        // return the difference between the product of its digits and the sum of its digits.\n        return product - sum;\n    }\n};\n```\n\n# Complexity\n- Time complexity: O(logN)\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```\n How can we derive the runtime?\n\n1000 / 10 = 100\n100 / 10  = 10\n10 / 10   = 1\n1/ 10     = 0\n\nit takes 4 steps\n\n10^(4-1) = 1000\n\n10^(n-1) is equivalent to log (n-1) # base 10\n\n log(n-1) ~= log n\n\n->TC = O(log n) # base 10\n```
['Math', 'C++']