Skip to content

Commit 74c81c0

Browse files
authored
Create subtract-the-product-and-sum-of-digits-of-an-integer.cpp
1 parent 0518c48 commit 74c81c0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int subtractProductAndSum(int n) {
7+
int product = 1, total = 0;
8+
for (; n; n /= 10) {
9+
product *= n % 10;
10+
total += n % 10;
11+
}
12+
return product - total;
13+
}
14+
};

0 commit comments

Comments
 (0)