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 the digit sum of `nums`.

30:00

Difference Between Element Sum and Digit Sum of an Array
easy
Free written solution
Topics
Companies

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 the digit sum of nums.

Example 1:
Input: {"nums":[1,15,6,3]}
Output: 9
Constraints:
  • 1≤nums.length≤20001 \leq nums.length \leq 2000

  • 1≤nums[i]≤20001 \leq nums[i] \leq 2000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,15,6,3]}

Two running totals start at 0. Each number goes into element_sum whole, and into digit_sum broken into its digits.

nums

whole number

its digits

[0]

1
1

[1]

15
1
5

[2]

6
6

[3]

3
3

element_sum

0

digit_sum

0

gap = element_sum - digit_sum

0

Variables
VariableValue
element_sum0
digit_sum0
DepthFunction Call
Stack empty
0/9