Welcome to Subscribe On Youtube

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

Description

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

Solutions

Solution 1: Simulation

We traverse the array $nums$, calculate the sum of elements $a$ and the sum of digits $b$, and finally return $ a - b $.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

  • 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);
        }
    }
    
  • class Solution {
    public:
        int differenceOfSum(vector<int>& nums) {
            int a = 0, b = 0;
            for (int x : nums) {
                a += x;
                for (; x; x /= 10) {
                    b += x % 10;
                }
            }
            return abs(a - b);
        }
    };
    
  • class Solution:
        def differenceOfSum(self, nums: List[int]) -> int:
            a, b = sum(nums), 0
            for x in nums:
                while x:
                    b += x % 10
                    x //= 10
            return abs(a - b)
    
    
  • func differenceOfSum(nums []int) int {
    	a, b := 0, 0
    	for _, x := range nums {
    		a += x
    		for ; x > 0; x /= 10 {
    			b += x % 10
    		}
    	}
    	return abs(a - b)
    }
    
    func abs(x int) int {
    	if x < 0 {
    		return -x
    	}
    	return x
    }
    
  • function differenceOfSum(nums: number[]): number {
        return nums.reduce((r, v) => {
            r += v;
            while (v !== 0) {
                r -= v % 10;
                v = Math.floor(v / 10);
            }
            return r;
        }, 0);
    }
    
    
  • impl Solution {
        pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
            let mut ans = 0;
            for &num in nums.iter() {
                let mut num = num;
                ans += num;
                while num != 0 {
                    ans -= num % 10;
                    num /= 10;
                }
            }
            ans
        }
    }
    
    

All Problems

All Solutions