Problem

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 _theabsolute 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|.

Examples

Example 1

1
2
3
4
5
6
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

1
2
3
4
5
6
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

Solution

Method 1 – Element and Digit Sum Calculation

Intuition

The element sum is simply the sum of all numbers in the array. The digit sum is the sum of all digits of all numbers. The answer is the absolute difference between these two sums.

Approach

  1. Initialize two variables: elem_sum for the sum of elements, and digit_sum for the sum of all digits.
  2. For each number in the array:
    • Add it to elem_sum.
    • Extract its digits and add each digit to digit_sum.
  3. Return the absolute difference between elem_sum and digit_sum.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    int differenceOfSum(vector<int>& nums) {
        int elem_sum = 0, digit_sum = 0;
        for (int x : nums) {
            elem_sum += x;
            int y = x;
            while (y) {
                digit_sum += y % 10;
                y /= 10;
            }
        }
        return abs(elem_sum - digit_sum);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func differenceOfSum(nums []int) int {
    elemSum, digitSum := 0, 0
    for _, x := range nums {
        elemSum += x
        y := x
        for y > 0 {
            digitSum += y % 10
            y /= 10
        }
    }
    if elemSum > digitSum {
        return elemSum - digitSum
    }
    return digitSum - elemSum
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int differenceOfSum(int[] nums) {
        int elemSum = 0, digitSum = 0;
        for (int x : nums) {
            elemSum += x;
            int y = x;
            while (y > 0) {
                digitSum += y % 10;
                y /= 10;
            }
        }
        return Math.abs(elemSum - digitSum);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    fun differenceOfSum(nums: IntArray): Int {
        var elemSum = 0
        var digitSum = 0
        for (x in nums) {
            elemSum += x
            var y = x
            while (y > 0) {
                digitSum += y % 10
                y /= 10
            }
        }
        return kotlin.math.abs(elemSum - digitSum)
    }
}
1
2
3
4
5
class Solution:
    def differenceOfSum(self, nums: list[int]) -> int:
        elem_sum = sum(nums)
        digit_sum = sum(int(d) for x in nums for d in str(x))
        return abs(elem_sum - digit_sum)
1
2
3
4
5
6
7
impl Solution {
    pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
        let elem_sum: i32 = nums.iter().sum();
        let digit_sum: i32 = nums.iter().map(|&x| x.to_string().chars().map(|d| d.to_digit(10).unwrap() as i32).sum::<i32>()).sum();
        (elem_sum - digit_sum).abs()
    }
}
1
2
3
4
5
6
7
class Solution {
    differenceOfSum(nums: number[]): number {
        const elemSum = nums.reduce((a, b) => a + b, 0);
        const digitSum = nums.reduce((a, x) => a + x.toString().split('').reduce((s, d) => s + Number(d), 0), 0);
        return Math.abs(elemSum - digitSum);
    }
}

Complexity

  • ⏰ Time complexity: O(n * m), where n is the length of nums and m is the number of digits in the largest number (at most 4).
  • 🧺 Space complexity: O(1), as only a few variables are used.