Skip to main content

Command Palette

Search for a command to run...

Minimum Sum of Four Digit Number After Splitting Digits

Published
2 min read
Minimum Sum of Four Digit Number After Splitting Digits
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 num consisting of exactly four digits. Split num into two new integers new1 and new2 by using the digits found in num. Leading zeros are allowed in new1 and new2, and all the digits found in num must be used.

  • For example, given num = 2932, you have the following digits: two 2's, one 9 and one 3. Some of the possible pairs [new1, new2] are [22, 93], [23, 92], [223, 9] and [2, 329].

Return the minimum possible sum of new1 and new2.

LeetCode Problem - 2160

import java.util.Arrays;

class Solution {
    // Method to find the minimum sum of a given number's digits
    public int minimumSum(int num) {
        // Create an array to store the digits
        int[] arr = new int[4];

        // Populate the array with the digits of the number
        for (int i = 0; i < arr.length; i++) {
            arr[i] = num % 10;  // Extract the last digit of the number
            num /= 10;          // Remove the last digit from the number
        }

        // Sort the array to easily find the smallest and largest digits
        Arrays.sort(arr);

        // Calculate the minimum sum:
        // Form the smallest two-digit number (10 * smallest digit + second smallest digit)
        // Form the largest two-digit number (10 * third smallest digit + largest digit)
        return (10 * arr[0] + arr[3]) + (10 * arr[1] + arr[2]);
    }
}

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.