You are given an integer number ‘N’, and your task is to return the difference between the product of its digits and the sum of its digits.
Note:You do not need to print anything; it has already been taken care of. Just implement the given function.
For Example :
Input: N = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
The first line of input contains a single integer ‘T’ denoting the number of test cases.
The second line contains the integer ‘N’ whose difference between the product and the sum of all the digits need to be calculated.
Output Format :
For each test case print the difference between the product and the sum of all the digits in ‘N’.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 3000
1 <= N <= 10^5
Time Limit: 1 sec
2
234
4421
15
21
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
Product of digits = 4 * 4 * 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
Result = 32 - 11 = 21
1
87
41
Think about how can all the digits of the number be extracted?
The idea here is to traverse through all the digits in the number and keep calculating the product and sum for each digit until the sum and products of all the digits are obtained.
The algorithm will be-
O(digits), where ‘digits’ denote the number of digits present in the given number.
Since we are finding the product and sum for all the digits present in the number, therefore, the time complexity will be O(digits).
O(1).
Since constant space is used.