Skip to main content

Difference Between Element Sum and Digit Sum of an Array - Solution & Explanation

EasyArrayMath14 min readAsked at: Amazon, Google, Innovaccer +1
Practice this problem

Problem Statement

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

Note that the absolute difference between two integers x and y is defined as |x - y|.

 

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 <= 2000
  • 1 <= nums[i] <= 2000

Approach Overview

Problem Overview: You are given an integer array nums. Compute two values: the sum of all elements in the array and the sum of every digit from those elements. The task is to return the absolute difference between these two sums.

Approach 1: Iterative Digit Extraction to Compute Sums (Time: O(n * d), Space: O(1))

This approach processes each number in the array and extracts its digits one by one. First, iterate through the array and accumulate the element sum. Then for each number, repeatedly apply num % 10 to get the last digit and add it to the digit sum, followed by num /= 10 to remove the digit. This continues until the number becomes zero. The key idea is that digit extraction can be done using simple arithmetic operations without converting numbers to strings. If the array has n elements and each number contains up to d digits, the time complexity becomes O(n * d). Space complexity remains O(1) since only running totals are stored. This is the most straightforward implementation and works well in any language.

Approach 2: Mathematical Handling by Modulo (Time: O(n * d), Space: O(1))

This version focuses on computing the difference directly using arithmetic properties instead of maintaining two separate totals. For each element, add the full number to the element sum while simultaneously subtracting its digits from a running difference using modulo operations. Each iteration extracts digits with % 10 and reduces the number using integer division by 10. The insight is that the difference between element sum and digit sum can be accumulated incrementally rather than calculated at the end. This keeps the logic tight and avoids maintaining multiple aggregates. Complexity remains O(n * d) because every digit of every number must still be processed, while space usage stays O(1). The algorithm relies purely on arithmetic operations commonly used in math problems.

Both approaches rely on simple iteration over the input array, a common pattern in array problems. Digit extraction using modulo is also a fundamental technique in many math and number manipulation questions.

Recommended for interviews: Interviewers typically expect the iterative digit extraction approach. It clearly demonstrates that you understand how to manipulate numbers using modulo and division. The mathematical accumulation variant is slightly cleaner but conceptually the same. Showing the basic digit extraction first proves your understanding, while writing it in a compact form shows implementation maturity.

Approach 1: Iterative Digit Extraction to Compute Sums

This approach uses iteration to extract each digit for calculating the digit sum. The element sum is simply the sum of all numbers. For the digit sum, convert each number to its constituent digits by using modulo and integer division.

This C solution iterates over the array to compute both the element sum and the digit sum. The digit extraction uses modulo and division to break down each integer into its digits.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * log(max(nums[i]))), where n is the number of elements and max(nums[i]) is the maximum number in nums. Space Complexity: O(1).

Try this approach in the editor →

Approach 2: Mathematical Handling by Modulo

This approach is the same iterative technique but focuses on optimizing the handling by utilizing mathematical operations directly to extract each digit, avoiding string conversions.

This C solution iteratively calculates the digit sum for each number using modulo and integer division, ensuring efficient digit extraction without string conversion.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * log(max(nums[i]))), where n is the number of elements and max(nums[i]) is the maximum number in nums. Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation

We traverse the array nums, calculate the sum of the elements x and the sum of the digits y, and finally return |x - y|. Since x is always greater than or equal to y, we can directly return x - y.

The time complexity is O(n times log_{10} M), where n and M are the length of the array nums and the maximum value of the elements in the array, respectively. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Digit Extraction to Compute Sums

Time Complexity: O(n * log(max(nums[i]))), where n is the number of elements and max(nums[i]) is the maximum number in nums. Space Complexity: O(1).

Mathematical Handling by Modulo

Time Complexity: O(n * log(max(nums[i]))), where n is the number of elements and max(nums[i]) is the maximum number in nums. Space Complexity: O(1).

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Digit Extraction to Compute SumsO(n * d)O(1)General case. Clear and easy to implement during interviews.
Mathematical Handling by ModuloO(n * d)O(1)When you want a concise solution that accumulates the difference directly.

Video Solution

2535. Difference Between Element Sum and Digit Sum of an Array | Weekly Contest 328 | LeetCode 2535 • Bro Coders • 1,694 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Difference Between Element Sum and Digit Sum of an Array easy or hard?
This problem is classified as Easy. It mainly tests understanding of array traversal and digit extraction using modulo arithmetic. Most solutions are short and run in O(n) time with constant space.
Difference Between Element Sum and Digit Sum of an Array Python/Java solution
In Python or Java, iterate through the array, accumulate the element sum, and extract digits using modulo and division inside a loop. The logic is identical across languages because it relies only on basic arithmetic operations and simple iteration.
How to solve Difference Between Element Sum and Digit Sum of an Array in O(n)?
Traverse the array once while maintaining two running totals: element sum and digit sum. Extract digits from each number using modulo (num % 10) and division (num /= 10). After processing all elements, return the absolute difference between the two sums. This effectively runs in linear time with constant space.
What is the best approach for Difference Between Element Sum and Digit Sum of an Array?
The standard approach iterates through the array and extracts digits using modulo operations. For each number, add the full value to the element sum and repeatedly use num % 10 and num / 10 to accumulate the digit sum. This runs in O(n * d) time where d is the number of digits per element and uses O(1) extra space.
Is Difference Between Element Sum and Digit Sum of an Array asked at Google/Amazon/Meta?
Problems involving digit extraction and arithmetic manipulation appear frequently in coding interviews at large tech companies. While this exact problem is relatively simple, similar number-processing questions are common screening exercises used by companies like Amazon and Google.
What data structure is used in Difference Between Element Sum and Digit Sum of an Array?
The primary data structure is a simple array traversal. The algorithm does not require additional structures like hash maps or stacks. It relies mainly on arithmetic operations such as modulo and integer division.
What is the time complexity of Difference Between Element Sum and Digit Sum of an Array?
The time complexity is O(n * d). The algorithm iterates through n numbers in the array, and for each number processes its digits individually. Since integers have a limited number of digits, this behaves close to O(n) in practice.

Ready to solve this problem?

Practice Difference Between Element Sum and Digit Sum of an Array with our built-in code editor and test cases.

Practice on FleetCode