Difference Between Element Sum and Digit Sum of an Array

Easy
272.

Difference Between Element Sum and Digit Sum of an Array

Difference Between Element Sum and Digit Sum of an Array is an easy coding problem on arrays and math, asked in Amazon and Infosys interviews.

You are given a positive integer array nums.

  • The element sum is the sum of all the elements.
  • The digit sum is the sum of all the digits, treating each element digit by digit.

Return the absolute difference between the element sum and the digit sum.

Example 1

Input
nums = [1,15,6,3]
Output
9
Explanation
The element sum is 25 and the digit sum is 1+1+5+6+3 = 16.

Example 2

Input
nums = [1,2,3,4]
Output
0
Explanation
Single-digit numbers make the two sums identical.

Example 3

Input
nums = [100]
Output
99

Constraints

  • 1 <= nums.length <= 40
  • 1 <= nums[i] <= 10000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var differenceOfSum = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Difference Between Element Sum and Digit Sum of an Array Solution

Difference Between Element Sum and Digit Sum of an Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Difference Between Element Sum and Digit Sum of an Array is an easy coding problem on arrays and math, asked in Amazon and Infosys interviews.

  • Difficulty: Easy
  • Topics: Arrays, Math
  • Asked at: Amazon, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a positive integer array nums.

  • The element sum is the sum of all the elements.
  • The digit sum is the sum of all the digits, treating each element digit by digit.

Return the absolute difference between the element sum and the digit sum.

Example 1

Input: nums = [1,15,6,3]
Output: 9
Explanation: The element sum is 25 and the digit sum is 1+1+5+6+3 = 16.

Example 2

Input: nums = [1,2,3,4]
Output: 0
Explanation: Single-digit numbers make the two sums identical.

Example 3

Input: nums = [100]
Output: 99

Constraints

  • 1 <= nums.length <= 40
  • 1 <= nums[i] <= 10000

Hints for Difference Between Element Sum and Digit Sum of an Array

  1. Hint 1

    Accumulate both sums in the same pass.

  2. Hint 2

    The digit sum never exceeds the element sum, so the absolute value is a formality — but keep it for safety.

How to solve Difference Between Element Sum and Digit Sum of an Array

Accumulate the element sum and the digit sum, then return the absolute difference.

Approach

  1. Sum the elements.
  2. Sum every decimal digit of every element.
  3. Return |elementSum - digitSum|.

Why it works

Both are direct folds over the input; the digit sum of a number never exceeds the number, so the difference is non-negative.

Complexity

  • Time — O(n · digits)
  • Space — O(1)

Pitfalls

  • Peel digits with % 10 / // 10 if you prefer to avoid strings.

Reference solution

Python

def differenceOfSum(nums) -> int:
    element_sum = sum(nums)
    digit_sum = sum(int(ch) for x in nums for ch in str(x))
    return abs(element_sum - digit_sum)

Java

public static int differenceOfSum(int[] nums) {
    int elementSum = 0, digitSum = 0;
    for (int v : nums) {
        elementSum += v;
        int x = v;
        while (x > 0) {
            digitSum += x % 10;
            x /= 10;
        }
    }
    return Math.abs(elementSum - digitSum);
}

C++

int differenceOfSum(vector<int>& nums) {
    int elementSum = 0, digitSum = 0;
    for (int v : nums) {
        elementSum += v;
        int x = v;
        while (x > 0) {
            digitSum += x % 10;
            x /= 10;
        }
    }
    return abs(elementSum - digitSum);
}

JavaScript

var differenceOfSum = function(nums) {
    let elementSum = 0, digitSum = 0;
    for (let i = 0; i < nums.length; i++) {
        elementSum += nums[i];
        let x = nums[i];
        while (x > 0) {
            digitSum += x % 10;
            x = Math.floor(x / 10);
        }
    }
    return Math.abs(elementSum - digitSum);
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays