Subtract the Product and Sum of Digits of an Integer

šŸ  ā¬…ļø āž”ļø

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

Note: This problem is from LeetCode.
āŒ Compiled
āŒ Executed
āŒ Correct
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
āŒ Compiled
āŒ Executed
āŒ Correct
```
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
```
šŸŒ Data from online sources
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
  1. Iterate through each query, and get the values of left, right, and k.
  2. Create an array count with 26 elements for each English lowercase letter.
  3. Iterate from left to right, count occurrences of each letter in the substring s[left...right].
  4. Calculate the number of odd counts of the letters.
  5. Determine if the number of odd counts divided by 2 is less than or equal to k. If true, append true to the result, otherwise append false.
  6. After all queries are processed, return the result array.
šŸŒ Data from online sources
#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;
}
  1. Iterate through each query, and get the values of left, right, and k.
  2. Create an array count with 26 elements for each English lowercase letter.
  3. Iterate from left to right, count occurrences of each letter in the substring s[left...right].
  4. Calculate the number of odd counts of the letters.
  5. Determine if the number of odd counts divided by 2 is less than or equal to k. If true, append true to the result, otherwise append false.
  6. After all queries are processed, return the result array.