Easy

Subtract the Product and Sum of Digits of an IntegerC++

Full explanation · Time O(logn) · Space O(1)

// Time:  O(n)
// Space: O(1)

class Solution {
public:
    int subtractProductAndSum(int n) {
        int product = 1, total = 0;
        for (; n; n /= 10) {
            product *= n % 10;
            total += n % 10;
        }
        return product - total;
    }
};