Skip to main content

Command Palette

Search for a command to run...

Difference Between Element Sum and Digit Sum of an Array

Published
2 min read
Difference Between Element Sum and Digit Sum of an Array
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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|.

LeetCode Problem - 2535

class Solution {

    // Method to calculate the sum of digits of a number
    public static int digiSum(int num) {
        int sum = 0;
        while(num > 0) {
            int rem = num % 10;
            sum = sum + rem;
            num = num / 10;
        }
        return sum;
    }

    // Method to find the absolute difference between the sum of elements and the sum of their digits
    public int differenceOfSum(int[] nums) {
        int eleSum = 0, digitSum = 0;

        // Loop through each element in the array
        for(int i = 0; i < nums.length; i++) {   
            int num = nums[i];

            // Add the element to the element sum
            eleSum += nums[i]; 

            // Calculate the sum of digits of the current element and add it to digitSum
            digitSum += Solution.digiSum(num);
        }

        // Return the absolute difference between the sum of elements and the sum of their digits
        return Math.abs(eleSum - digitSum);
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.