Given an integer number n
, return the difference between the product of its digits and the sum of its digits.
Example 1:
Input: n = 234 Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15
Example 2:
Input: n = 4421 Output: 21 Explanation: Product of digits = 4 * 4 * 2 * 1 = 32 Sum of digits = 4 + 4 + 2 + 1 = 11 Result = 32 - 11 = 21
Constraints:
1 <= n <= 10^5
program main
implicit none
integer, parameter :: n = 234
integer :: result
result = digit_product_minus_digit_sum(n)
write (*, '(I0)') result
contains
function digit_product_minus_digit_sum(n) result(diff)
implicit none
integer, intent(in) :: n
integer :: diff, digit_product, digit_sum
digit_product = 1
digit_sum = 0
do while (n > 0)
digit_product = digit_product * mod(n, 10)
digit_sum = digit_sum + mod(n, 10)
n = n / 10
end do
diff = digit_product - digit_sum
end function digit_product_minus_digit_sum
end program main
temp.f95:23:12: 23 | n = n / 10 | 1 Error: Dummy argument ānā with INTENT(IN) in variable definition context (assignment) at (1)
```
program diff_product_sum
implicit none
integer :: n, product, sum, result
! Read input
read(*,*) n
! Calculate product of digits
product = 1
do while (n > 0)
product = product * mod(n, 10)
n = n / 10
end do
! Calculate sum of digits
sum = 0
do while (n > 0)
sum = sum + mod(n, 10)
n = n / 10
end do
! Calculate result
result = product - sum
! Output result
write(*,*) result
end program diff_product_sum
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:30:2: 30 | ``` | 1 Error: Invalid character in name at (1)
def can_make_pali_queries(s, queries):
result = []
for left, right, k in queries:
count = [0] * 26
for i in range(left, right + 1):
count[ord(s[i]) - ord('a')] += 1
odd_count = sum(c % 2 for c in count)
result.append(odd_count // 2 <= k)
return result
#include <vector>
#include <string>
std::vector<bool> canMakePaliQueries(std::string s, std::vector<std::vector<int>>& queries) {
std::vector<bool> result;
for (auto& query : queries) {
int left = query[0], right = query[1], k = query[2];
int count[26] = {0};
for (int i = left; i <= right; i++) {
count[s[i] - 'a']++;
}
int odd_count = 0;
for (int i = 0; i < 26; i++) {
odd_count += count[i] % 2;
}
result.push_back(odd_count / 2 <= k);
}
return result;
}