Skip to main content

Command Palette

Search for a command to run...

Minimum Element After Replacement With Digit Sum

Published
1 min read
Minimum Element After Replacement With Digit Sum
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 an integer array nums.

You replace each element in nums with the sum of its digits.

Return the minimum element in nums after all replacements.

LeetCode Problem - 3300

class Solution {

    // Method to find the minimum element in the array after replacing each element with the sum of its digits
    public int minElement(int[] nums) {
        int minElement = Integer.MAX_VALUE;  // Initialize the minimum element with the largest possible integer value

        // Loop through the array and replace each element with its digit sum
        for (int i = 0; i < nums.length; i++) {
            nums[i] = digitSum(nums[i]);  // Calculate the digit sum of the current element

            // Update the minimum element if the current digit sum is smaller
            if (nums[i] < minElement) {
                minElement = nums[i];
            }
        }

        return minElement;  // Return the smallest digit sum
    }

    // Helper method to calculate the sum of the digits of a given number
    public int digitSum(int num) {
        int sum = 0;  // Initialize the sum of digits

        // Loop to calculate the sum of digits
        while (num != 0) {
            sum += num % 10;  // Add the last digit to the sum
            num /= 10;  // Remove the last digit from the number
        }

        return sum;  // Return the total sum of digits
    }
}

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.