Menu Close

Leetcode 2535: Difference Between Element Sum and Digit Sum of an Array Solution

Here, we will see how to solve Difference Between Element Sum and Digit Sum of an Array Solution of leet code 2535 problem.

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.

You have to 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 = [2,16,7,4]
Output: 9
Explanation: 
The element sum of nums is 2 + 16 + 7 + 4 = 29.
The digit sum of nums is 2 + 1 + 6 + 7 + 4 = 20.
The absolute difference between the element sum and digit sum is |29 - 20| = 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.

Approach:

  • Take two sum variable. One sum variable stores total sum of elements of an array and second sum variable stores total digit sum of each elements of array.
  • Iterate through vector
    • Store element sum in total_sum variable
    • Store digit sum of each elements in total_digit_sum variable
  • Get the difference between total_sum and total_digit_sum variable value

Difference Between Element Sum and Digit Sum of an Array Solution in C++ and Go:

Here, we will be solving problem in multiple ways with code.

C++ code 1:

class Solution {
public:
    int differenceOfSum(vector<int>& nums) {
        int total_sum = 0;
        int total_digit_sum = 0;
        
        for(int i = 0; i < nums.size(); i++) {
            // Store all the array elements sum
            total_sum += nums[i];
            
            int n = nums[i], digit_sum = 0;
            
            // Store the digit sums of each elemenet of array
            while(n) {
                digit_sum += n % 10;
                n /= 10;
            }
            total_digit_sum += digit_sum;
        }
        // Get the diff between total sum total digit sum
        return abs(total_digit_sum - total_sum);
    }
};

Go code 1:

func differenceOfSum(nums []int) int {
    var total_sum = 0;
    var total_digit_sum = 0;
        
    for i := 0; i < len(nums); i++ {
        // Store all the array elements sum
        total_sum += nums[i];
            
        var n, digit_sum = nums[i], 0;
            
        // Store the digit sums of each elemenet of array
        for n != 0 {
            digit_sum += n % 10;
            n /= 10;
        }
        total_digit_sum += digit_sum;
    }
    // Get the diff between total sum total digit sum
    return int(math.Abs(float64(total_digit_sum - total_sum)))
}

Output:

Input: nums = [2,16,7,4]
Output: 9

Time complexity: O(n)

Space complexity: O(1)

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

https://www.techieindoor.com/category/interview-questions/

Posted in C++, Easy, golang, golang program, Leetcode

Leave a Reply

Your email address will not be published. Required fields are marked *