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

In this guide, you will get 2535. Difference Between Element Sum and Digit Sum of an Array LeetCode Solution with the best time and space complexity. The solution to Difference Between Element Sum and Digit Sum of an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Difference Between Element Sum and Digit Sum of an Array solution in C++
  4. Difference Between Element Sum and Digit Sum of an Array solution in Java
  5. Difference Between Element Sum and Digit Sum of an Array solution in Python
  6. Additional Resources
2535. Difference Between Element Sum and Digit Sum of an Array LeetCode Solution image

Problem Statement of Difference Between Element Sum and Digit Sum of an Array

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.

See also  2729. Check if The Number is Fascinating LeetCode Solution

Constraints:

1 <= nums.length <= 2000
1 <= nums[i] <= 2000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2535. Difference Between Element Sum and Digit Sum of an Array LeetCode Solution in C++

class Solution {
 public:
  int differenceOfSum(vector<int>& nums) {
    const int elementSum = accumulate(nums.begin(), nums.end(), 0);
    const int allDigitSum = getAllDigitSum(nums);
    return abs(elementSum - allDigitSum);
  }

 private:
  int getAllDigitSum(const vector<int>& nums) {
    int allDigitSum = 0;
    for (const int num : nums)
      allDigitSum += getDigitSum(num);
    return allDigitSum;
  }

  int getDigitSum(int num) {
    int digitSum = 0;
    while (num > 0) {
      digitSum += num % 10;
      num /= 10;
    }
    return digitSum;
  }
};
/* code provided by PROGIEZ */

2535. Difference Between Element Sum and Digit Sum of an Array LeetCode Solution in Java

class Solution {
  public int differenceOfSum(int[] nums) {
    final int elementSum = Arrays.stream(nums).sum();
    final int allDigitSum = getAllDigitSum(nums);
    return Math.abs(elementSum - allDigitSum);
  }

  private int getAllDigitSum(int[] nums) {
    int allDigitSum = 0;
    for (final int num : nums)
      allDigitSum += getDigitSum(num);
    return allDigitSum;
  }

  private int getDigitSum(int num) {
    int digitSum = 0;
    while (num > 0) {
      digitSum += num % 10;
      num /= 10;
    }
    return digitSum;
  }
}
// code provided by PROGIEZ

2535. Difference Between Element Sum and Digit Sum of an Array LeetCode Solution in Python

class Solution:
  def differenceOfSum(self, nums: list[int]) -> int:
    elementSum = sum(nums)
    digitSum = self._getAllDigitSum(nums)
    return abs(elementSum - digitSum)

  def _getAllDigitSum(self, nums: list[int]) -> int:
    return sum(self._getDigitSum(num) for num in nums)

  def _getDigitSum(self, num: int) -> int:
    return sum(int(digit) for digit in str(num))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.