Difference Between Element Sum and Digit Sum of an Array
EasyUpdated: Aug 2, 2025
Practice on:
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
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 <= 20001 <= 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
- Initialize two variables:
elem_sumfor the sum of elements, anddigit_sumfor the sum of all digits. - For each number in the array:
- Add it to
elem_sum. - Extract its digits and add each digit to
digit_sum.
- Add it to
- Return the absolute difference between
elem_sumanddigit_sum.
Code
C++
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);
}
};
Go
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
}
Java
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);
}
}
Kotlin
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)
}
}
Python
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)
Rust
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()
}
}
TypeScript
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.