Difference Between Element Sum and Digit Sum of an Array

🏠 ⬅️ ➑️

You are given a positive integer array nums.

  • The element sum is the sum of all the elements in nums.
  • The digit sum is the sum of all the digits (not necessarily distinct) that appear in nums.

Return the absolute difference between the element sum and digit sum of nums.

Note that the absolute difference between two integers x and y is defined as |x - y|.

Example 1:

Input: nums = [1,15,6,3] Output: 9 Explanation: The element sum of nums is 1 + 15 + 6 + 3 = 25. The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16. The absolute difference between the element sum and digit sum is |25 - 16| = 9.

Example 2:

Input: nums = [1,2,3,4] Output: 0 Explanation: The element sum of nums is 1 + 2 + 3 + 4 = 10. The digit sum of nums is 1 + 2 + 3 + 4 = 10. The absolute difference between the element sum and digit sum is |10 - 10| = 0.

Constraints:

  • 1 <= nums.length <= 2000
  • 1 <= nums[i] <= 2000

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 4
    integer, dimension(n) :: nums
    integer :: i

    ! Examples
    nums = reshape([1, 15, 6, 3], shape(nums))
    write(*,*) 'Example 1:'
    write(*,*) 'Input:', nums
    write(*,*) 'Output:', abs(sum(nums) - digitSum(nums))

    nums = reshape([1, 2, 3, 4], shape(nums))
    write(*,*) 'Example 2:'
    write(*,*) 'Input:', nums
    write(*,*) 'Output:', abs(sum(nums) - digitSum(nums))

contains

    function digitSum(arr) result(res)
        implicit none
        integer, intent(in) :: arr(:)
        integer :: res
        integer :: i, j

        res = 0
        do i = 1, size(arr)
            do j = 1, len_trim(str(arr(i)))
                res = res + ichar(str(arr(i))(j:j)) - ichar('0')
            end do
        end do
    end function digitSum

end program main
❌ Compiled
❌ Executed
❌ Correct
module abs_diff_sums
    implicit none
    private
    public :: abs_diff_sums

contains

    function abs_diff_sums(nums) result(diff)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: diff, element_sum, digit_sum

        element_sum = sum(nums)
        digit_sum = sum(digits(nums))
        diff = abs(element_sum - digit_sum)
    end function abs_diff_sums

    pure function digits(nums) result(digits_array)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: digits_array(size(nums))

        integer :: i, digit

        do i = 1, size(nums)
            digit = nums(i)
            do while (digit > 0)
                digits_array(i) = digits_array(i) + mod(digit, 10)
                digit = digit / 10
            end do
        end do
    end function digits
end module abs_diff_sums

program test_abs_diff_sums
    use abs_diff_sums
    implicit none
    integer :: nums(4)

    nums = [1, 15, 6, 3]
    write (*,*) 'Example 1:', abs_diff_sums(nums)

    nums = [1, 2, 3, 4]
    write (*,*) 'Example 2:', abs_diff_sums(nums)
end program test_abs_diff_sums
🌐 Data from online sources
def difference_between_element_and_digit_sum(nums):
    element_sum = sum(nums)
    digit_sum = sum(sum(int(digit) for digit in str(num)) for num in nums)
    return abs(element_sum - digit_sum)

The idea here is to calculate the element sum and digit sum in one pass of the input list. We iterate through the input list, adding each number to the element sum. To get the digit sum, we keep dividing the number by 10 and adding the remainder to the digit sum until the number becomes 0. Once we have the element sum and digit sum, we can return the absolute difference between them. The specific implementation will vary slightly between different languages, but the overall algorithm should be the same.

🌐 Data from online sources
int differenceBetweenElementAndDigitSum(vector<int>& nums) {
    int element_sum = 0, digit_sum = 0;
    for (int num : nums) {
        element_sum += num;
        while (num > 0) {
            digit_sum += num % 10;
            num /= 10;
        }
    }
    return abs(element_sum - digit_sum);
}

The idea here is to calculate the element sum and digit sum in one pass of the input list. We iterate through the input list, adding each number to the element sum. To get the digit sum, we keep dividing the number by 10 and adding the remainder to the digit sum until the number becomes 0. Once we have the element sum and digit sum, we can return the absolute difference between them. The specific implementation will vary slightly between different languages, but the overall algorithm should be the same.