Difference Between Element Sum and Digit Sum of an Array
EasyPrompt
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 <= 20001 <= nums[i] <= 2000
Approaches
3 approaches with complexity analysis and trade-offs.
This is a straightforward but least efficient approach. It calculates the element sum by iterating through the array. For the digit sum, it converts each number to a string and then iterates through the characters of the string, summing up their numeric values. This method is generally slower due to the overhead associated with string manipulation.
Algorithm
- Initialize
elementSumanddigitSumto 0. - Iterate through each number
numin the input arraynums. - Add the
numtoelementSum. - Convert
numto its string representation. - Iterate through each character of the string.
- Convert the character back to an integer (e.g.,
c - '0') and add it todigitSum. - After the loop, return the absolute difference between
elementSumanddigitSum.
Walkthrough
This approach calculates the digit sum by first converting each number into its string representation. Then, it iterates through the characters of the string, converts each character back to a digit, and adds it to the digit sum. The element sum can be calculated in the same loop or a separate one. While intuitive, this method is generally slower due to the overhead associated with string manipulation.
Here is an implementation combining both calculations in a single loop:
class Solution { public int differenceOfSum(int[] nums) { int elementSum = 0; int digitSum = 0; for (int num : nums) { elementSum += num; String s = String.valueOf(num); for (char c : s.toCharArray()) { digitSum += c - '0'; } } return Math.abs(elementSum - digitSum); }}Complexity
Time
O(N * D), where N is the length of the array and D is the maximum number of digits in a number. While asymptotically similar to other approaches, the constant factors are higher due to string operations, making it slower in practice.
Space
O(D), for storing the string representation of a number. Since D, the maximum number of digits, is small and constant (max 4 for numbers up to 2000), this is effectively O(1) space.
Trade-offs
Pros
The logic for extracting digits might be intuitive for those familiar with string manipulation.
Cons
Significantly less performant than arithmetic-based approaches due to the overhead of string creation and character parsing.
Solutions
Solution
class Solution {public int differenceOfSum(int[] nums) { int a = 0, b = 0; for (int x : nums) { a += x; for (; x > 0; x /= 10) { b += x % 10; } } return Math.abs(a - b); }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.