#2535

Difference Between Element Sum and Digit Sum of an Array

newbie · 120 · lc easy +12 · verified · 85.3% accepted · 811 likes · top 96%

Description

Given a positive integer array nums, compute two quantities: the element sum (sum of all integers in the array) and the digit sum (sum of every individual digit across all integers in nums). Return the absolute value of their difference.

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.

Code

1
2
3