Skip to main content

Command Palette

Search for a command to run...

Maximum Difference by Remapping a Digit

Published
2 min read
Maximum Difference by Remapping a Digit
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 num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.

Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.

Notes:

  • When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2.

  • Bob can remap a digit to itself, in which case num does not change.

  • Bob can remap different digits for obtaining minimum and maximum values respectively.

  • The resulting number after remapping can contain leading zeroes.

LeetCode Problem - 2566

class Solution {
    public int minMaxDifference(int num) {
        // Find the difference between the maximum and minimum possible values
        // formed by modifying digits of the input number.
        return firstNonZeroAndNine(num);
    }

    public int firstNonZeroAndNine(int num) {
        // Convert the number to a string for digit manipulation.
        String strNum = String.valueOf(num);

        // Initialize placeholders for the first non-'9' and non-'0' characters.
        char non9 = '9';
        char non0 = '0';

        // Find the first digit that is not '9' (to replace with '9' later).
        for (int i = 0; i < strNum.length(); i++) {
            char temp = strNum.charAt(i);
            if (non9 != temp) { 
                // Assign the first non-'9' digit found.
                non9 = temp;
                break;
            }
        }

        // Find the first digit that is not '0' (to replace with '0' later).
        for (int i = 0; i < strNum.length(); i++) {
            char temp = strNum.charAt(i);
            if (non0 != temp) { 
                // Assign the first non-'0' digit found.
                non0 = temp;
                break;
            }
        }

        // Replace all occurrences of `non9` with '9' to form the highest number.
        int highest = Integer.parseInt(strNum.replace(non9, '9'));

        // Replace all occurrences of `non0` with '0' to form the lowest number.
        int lowest = Integer.parseInt(strNum.replace(non0, '0'));

        // Return the absolute difference between the highest and lowest numbers.
        return Math.abs(highest - lowest);
    }
}

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.