Skip to main content

Command Palette

Search for a command to run...

Find the Key of the Numbers

Published
2 min read
Find the Key of the Numbers
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 three positive integers num1, num2, and num3.

The key of num1, num2, and num3 is defined as a four-digit number such that:

  • Initially, if any number has less than four digits, it is padded with leading zeros.

  • The i<sup>th</sup> digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the i<sup>th</sup> digits of num1, num2, and num3.

Return the key of the three numbers without leading zeros (if any).

LeetCode Problem - 3270

class Solution {
    // Method to generate a key based on the smallest digits from three numbers
    public int generateKey(int num1, int num2, int num3) {

        // Convert the input numbers to 4-digit strings (padded with leading zeros if needed)
        String str1 = numToStr(num1);
        String str2 = numToStr(num2);
        String str3 = numToStr(num3);

        // Use StringBuilder to construct the resulting key
        StringBuilder sb = new StringBuilder();

        // Iterate through each digit position (0 to 3)
        for (int i = 0; i < 4; i++) {
            // Extract the digit at position 'i' for each number
            int e1 = str1.charAt(i) - '0'; // Convert character to integer
            int e2 = str2.charAt(i) - '0'; // Convert character to integer
            int e3 = str3.charAt(i) - '0'; // Convert character to integer

            // Find the smallest digit among the three numbers at this position
            int temp = Math.min(e1, e2);
            int temp1 = Math.min(temp, e3);

            // Append the smallest digit to the result
            sb.append(temp1);
        }

        // Convert the final constructed string into an integer and return it
        return Integer.parseInt(sb.toString());
    }

    // Helper method to convert a number to a 4-digit string with leading zeros if necessary
    public String numToStr(int num) {
        String result = String.format("%04d", num); // Format as a 4-digit string
        return result;
    }
}

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.