Difficulty: Easy | Acceptance: 85.30% | Paid: No Topics: Array, Math
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 that appear in nums. Return the absolute difference between element sum and digit sum.
Note that the digit sum of an integer is the sum of its digits (e.g., the digit sum of 12 is 1 + 2 = 3).
- Examples
- Constraints
- Iterative with String Conversion
- Iterative with Modulo Arithmetic
- Functional One-Pass
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 <= 2000
1 <= nums[i] <= 2000
Iterative with String Conversion
Intuition Convert each number to a string to easily iterate over its characters, which represent the digits, and sum them up.
Steps
- Initialize
element_sumanddigit_sumto 0. - Iterate through each number in the array.
- Add the number to
element_sum. - Convert the number to a string, iterate through characters, convert back to integer, and add to
digit_sum. - Return the absolute difference between the two sums.
class Solution:
def differenceOfSum(self, nums: list[int]) -> int:
element_sum = 0
digit_sum = 0
for num in nums:
element_sum += num
for d in str(num):
digit_sum += int(d)
return abs(element_sum - digit_sum)
Complexity
- Time: O(N * L) where N is the number of elements and L is the average number of digits.
- Space: O(L) to store the string representation of the number.
- Notes: Readable but incurs overhead for string allocation.
Iterative with Modulo Arithmetic
Intuition Use mathematical operations (modulo 10 and integer division by 10) to extract digits without converting to strings.
Steps
- Initialize
element_sumanddigit_sumto 0. - Iterate through each number in the array.
- Add the number to
element_sum. - While the number is greater than 0, add
num % 10todigit_sumand dividenumby 10. - Return the absolute difference.
class Solution:
def differenceOfSum(self, nums: list[int]) -> int:
element_sum = 0
digit_sum = 0
for num in nums:
element_sum += num
temp = num
while temp > 0:
digit_sum += temp % 10
temp //= 10
return abs(element_sum - digit_sum)
Complexity
- Time: O(N * L) where N is the number of elements and L is the average number of digits.
- Space: O(1) extra space.
- Notes: More efficient than string conversion as it avoids memory allocation.
Functional One-Pass
Intuition
Utilize built-in functional programming constructs like reduce, map, or sum to calculate the sums concisely.
Steps
- Calculate the element sum using the languageās standard sum/reduce function.
- Calculate the digit sum by mapping each number to its digits and summing them up.
- Return the absolute difference.
class Solution:
def differenceOfSum(self, nums: list[int]) -> int:
element_sum = sum(nums)
digit_sum = sum(int(d) for n in nums for d in str(n))
return abs(element_sum - digit_sum)
Complexity
- Time: O(N * L) where N is the number of elements and L is the average number of digits.
- Space: O(1) or O(N) depending on implementation details (e.g., intermediate streams).
- Notes: Concise and expressive, leveraging standard library features.