O(N) Easy approach | Beats 100% | Java
IntuitionThe problem requires us to determine the maximum and minimum values that can be formed by remapping exactly one digit in the given number. To achieve t
![image.png](https://assets.leetcode.com/users/images/6e9f7bef-3a97-4d28-a1cf-208ab82dea05_1739942149.0614734.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to determine the maximum and minimum values that can be formed by remapping exactly one digit in the given number. To achieve this, we must replace all occurrences of a chosen digit with another digit. The goal is to maximize one version of the number and minimize another, then return the difference.

- For the maximum value: We replace the leftmost non-9 digit with 9, since 9 is the highest possible digit.
- For the minimum value: We replace the leftmost non-0 digit with 0, since 0 is the lowest possible digit (even if it creates leading zeros).

By performing these two transformations, we obtain the maximum and minimum numbers and compute their difference.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Extract Digits: Convert the given number into an array of digits while maintaining the original order.
2. Find the Digit to Replace for Maximum Value:
    -  Identify the first non-9 digit from the left.
    - Replace all occurrences of this digit with 9.
3. Find the Digit to Replace for Minimum Value:
    - Identify the first non-0 digit from the left.
    - Replace all occurrences of this digit with 0.
4. Convert the Modified Arrays Back to Integers:
    - Construct the new numbers from the modified digit arrays.
5. Return the Difference: Compute maxNum - minNum.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(N)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(N) [N is the number of digits in num]
# Code
```java []
class Solution {
    public int minMaxDifference(int num) {
        int maxDigit = -1; // Stores the first non-9 digit (for max transformation)
        int minDigit = -1; // Stores the first non-0 digit (for min transformation)

        // Determine the number of digits in num
        int length = (int) Math.log10(num) + 1;  
        int[] digits = new int[length]; // Array to store digits of num in left-to-right order

        // Extract digits from num and store them in the correct order
        for (int i = length - 1; i >= 0; i--) {
            digits[i] = num % 10; // Extract last digit
            num /= 10; // Remove last digit
        }

        // Find the first non-9 digit from the left (for max transformation)
        for (int i = 0; i < length; i++) {
            if (digits[i] != 9) {
                maxDigit = digits[i]; // Store the digit to be replaced
                break;
            }
        }

        // Find the first non-0 digit from the left (for min transformation)
        for (int i = 0; i < length; i++) {
            if (digits[i] != 0) {
                minDigit = digits[i]; // Store the digit to be replaced
                break;
            }
        }

        // Create a copy of the digits array for the min transformation
        int[] digitsMin = Arrays.copyOf(digits, length);

        // Replace all occurrences of maxDigit with 9 to get the maximum number
        for (int i = 0; i < length; i++) {
            if (digits[i] == maxDigit) {
                digits[i] = 9;
            }
        }

        // Replace all occurrences of minDigit with 0 to get the minimum number
        for (int i = 0; i < length; i++) {
            if (digitsMin[i] == minDigit) {
                digitsMin[i] = 0;
            }
        }

        // Convert the transformed digit arrays back to integer values
        int minNum = 0, maxNum = 0;
        for (int i = 0; i < length; i++) {
            maxNum = maxNum * 10 + digits[i]; // Construct max number
            minNum = minNum * 10 + digitsMin[i]; // Construct min number
        }

        // Return the difference between the max and min values
        return maxNum - minNum;
    }
}


```
['Java']