Maximum Difference by Remapping a Digit

Tags : leetcode, math, cpp, easy

You are given an integer num. You know that Danny Mittal will sneakily remap one of the 10 possible digits (0 to 9) to another digit.

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

Notes:

Examples #

Example 1:

Input: num = 11891
Output: 99009
Explanation: 
To achieve the maximum value, Danny can remap the digit 1 to the digit 9 to yield 99899.
To achieve the minimum value, Danny can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.

Example 2:

Input: num = 90
Output: 99
Explanation:
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we return 99.

Constraints #

Solutions #


class Solution {
public:
    int minMaxDifference(int num) {
        string n = to_string(num), n1,n2;
        n1 = n, n2 = n;
        char c1=n[0],c2=n[0];
        for(char c: n) {
            if(c == '9') {
                continue;
            }
            else {
                c1 = c;
                break;
            }
        }
        for(int i = 0; i < n.length(); i++) {
            if(n1[i] == c1) {
                n1[i] = '9';
            }
            if(n2[i] == c2) {
                n2[i] = '0';
            }
        }
        int min = stoi(n2);
        int max = stoi(n1);
        return max - min;
    }
};