Difference Between Element Sum and Digit Sum of an Array (LeetCode 2535): Solution and Explanation

Easy
Array
Math

Given an array of positive integers, return the absolute difference between the sum of the elements and the sum of all their digits.

Intuition

Each element is counted twice: once as a whole number and once as a handful of loose digits. Both totals can grow side by side in a single pass.

Splitting a number into digits is the usual pair of moves: x % 10 reads the last digit and x //= 10 drops it.

Single-digit numbers add the same amount to both totals, so they never change the gap. Only numbers with two or more digits do, because a tens digit is worth ten times more inside the number than on its own.

Approach: One pass, two totals

  1. Set element_sum = 0 and digit_sum = 0.
  2. For each x in nums, add x to element_sum.
  3. While x > 0, add x % 10 to digit_sum and set x //= 10.
  4. Return abs(element_sum - digit_sum).

Step-by-step example

nums = [1, 15, 6, 3]
  1. x = 1: element_sum = 1, digit_sum = 1.
  2. x = 15: element_sum = 16, digits 1 + 5 make digit_sum = 7.
  3. x = 6: element_sum = 22, digit_sum = 13.
  4. x = 3: element_sum = 25, digit_sum = 16.
  5. Return |25 - 16| = 9. All of the gap came from 15, which counts 15 whole but only 6 as digits.

Code: Python, Java, C++

1class Solution:
2    def differenceOfSum(self, nums: List[int]) -> int:
3        element_sum = 0
4        digit_sum = 0
5        for x in nums:
6            element_sum += x
7            while x > 0:
8                digit_sum += x % 10
9                x //= 10
10        return abs(element_sum - digit_sum)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log M)

Each of the n numbers is split into its digits, and a number up to M has about log10(M) digits, at most 4 here.

Space

O(1)

Two running totals and a loop variable.

Other approaches

String digits (time O(n log M), space O(log M))

Convert each number to a string and add up int(c) for each character. Same complexity, a little more allocation.

Sum the per-number gaps (time O(n log M), space O(1))

Add up x - digitsum(x) for every x. It gives the same answer and makes it obvious that single-digit numbers contribute nothing.

Common mistakes

  • Modifying the loop variable before adding it to the element sum, which adds 0 instead of the number.
  • In C#, writing foreach (int x in nums) and then changing x does not compile, because a foreach variable is read-only. Copy it into a local first.
  • Forgetting Math.floor on the division in JavaScript, which never lets x reach 0 cleanly.

Frequently asked questions

Can the digit sum ever be larger than the element sum?

No. Each number is at least as large as the sum of its own digits, so the element sum is always greater than or equal to the digit sum. The abs is just a safe habit.

Can the totals overflow a 32-bit integer?

No. At most 2000 numbers of at most 2000 each gives 4,000,000, far below the 32-bit limit.

Why is the answer always a multiple of 9?

A number and its digit sum leave the same remainder when divided by 9, so each number minus its digit sum is a multiple of 9, and so is the total gap.