Skip to content
LC-2535 Easy LeetCode

2535. Difference Between Element Sum and Digit Sum of an Array

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 85% Topics: Array, Math
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogr), r = max(nums)
# Space: O(1)

# array
class Solution(object):
    def differenceOfSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def total(x):
            result = 0
            while x:
                result += x%10
                x //= 10
            return result

        return abs(sum(nums)-sum(total(x) for x in nums))

Solution from kamyu104/LeetCode-Solutions · MIT