You are given a positive integer array nums
.
nums
.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
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
temp.f95:30:46: 30 | res = res + ichar(str(arr(i))(j:j)) - ichar('0') | 1 Error: Syntax error in argument list at (1) temp.f95:29:31: 29 | do j = 1, len_trim(str(arr(i))) | 1 Error: Function βstrβ at (1) has no IMPLICIT type
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
temp.f95:4:27: 4 | public :: abs_diff_sums | 1 Error: PUBLIC attribute applied to MODULE abs_diff_sums at (1) temp.f95:8:26: 8 | function abs_diff_sums(nums) result(diff) | 1 Error: MODULE attribute of βabs_diff_sumsβ conflicts with PROCEDURE attribute at (1) temp.f95:9:21: 9 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:10:38: 10 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:47: 11 | integer :: diff, element_sum, digit_sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:31: 13 | element_sum = sum(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:37: 14 | digit_sum = sum(digits(nums)) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:43: 15 | diff = abs(element_sum - digit_sum) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:7: 16 | end function abs_diff_sums | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:21: 19 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:36:9: 36 | use abs_diff_sums | 1 Fatal Error: Cannot open module file βabs_diff_sums.modβ for reading at (1): No such file or directory compilation terminated.
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.
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.