You are given an integer num. You will apply the following steps to num two separate times:
x (0 <= x <= 9).y (0 <= y <= 9). Note y can be equal to x.x in the decimal representation of num by y.Let a and b be the two results from applying the operation to num independently.
Return the max difference between a and b.
Note that neither a nor b may have any leading zeros, and must not be 0.
Example 1:
Input: num = 555 Output: 888 Explanation: The first time pick x = 5 and y = 9 and store the new integer in a. The second time pick x = 5 and y = 1 and store the new integer in b. We have now a = 999 and b = 111 and max difference = 888
Example 2:
Input: num = 9 Output: 8 Explanation: The first time pick x = 9 and y = 9 and store the new integer in a. The second time pick x = 9 and y = 1 and store the new integer in b. We have now a = 9 and b = 1 and max difference = 8
Constraints:
1 <= num <= 108When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method solves this problem by trying every single possible change we can make to the number. We explore all combinations of replacements and pick the pair that gives us the biggest difference.
Here's how the algorithm would work step-by-step:
def max_difference_brute_force(number):
number_string = str(number)
max_difference = 0
for index in range(len(number_string)):
# Try replacing the current digit with 9
new_number_string_max = list(number_string)
digit_to_replace = number_string[index]
for i in range(len(new_number_string_max)):
if new_number_string_max[i] == digit_to_replace:
new_number_string_max[i] = '9'
new_number_max = int("".join(new_number_string_max))
max_difference = max(max_difference, abs(number - new_number_max))
# Handle leading zero case for min replacement
if index == 0:
replacement_digit = '1'
else:
replacement_digit = '0'
# Try replacing the current digit with 0 or 1
new_number_string_min = list(number_string)
for i in range(len(new_number_string_min)):
if new_number_string_min[i] == digit_to_replace:
new_number_string_min[i] = replacement_digit
new_number_min = int("".join(new_number_string_min))
max_difference = max(max_difference, abs(number - new_number_min))
return max_differenceThe goal is to find the biggest and smallest possible numbers you can create by replacing a single digit in the original number. We do this by strategically finding the digits to change to maximize and minimize the result without exploring every possible combination.
Here's how the algorithm would work step-by-step:
def max_diff(number):
number_string = str(number)
def find_max(number_string):
for digit_index, digit in enumerate(number_string):
if digit != '9':
# Replace first non-9 digit with 9.
digit_to_replace = digit
break
else:
return int(number_string)
maximized_string = number_string.replace(digit_to_replace, '9')
return int(maximized_string)
def find_min(number_string):
if number_string[0] != '1':
# Replace leading digit with 1.
digit_to_replace = number_string[0]
minimized_string = number_string.replace(digit_to_replace, '1')
return int(minimized_string)
else:
# Find next digit to replace with 0.
for digit_index, digit in enumerate(number_string[1:]):
if digit != '0' and digit != '1':
# Replace the first non-0 and non-1 with 0.
digit_to_replace = digit
break
else:
return int(number_string)
minimized_string = number_string.replace(digit_to_replace, '0')
return int(minimized_string)
maximized_number = find_max(number_string)
minimized_number = find_min(number_string)
# The difference gives us the maximum possible difference.
return maximized_number - minimized_number| Case | How to Handle |
|---|---|
| Null or negative input integer | Return 0 or throw IllegalArgumentException as the problem implies a positive integer input. |
| Single digit input (e.g., 5) | Replacing the single digit with another digit will necessarily result in a smaller number, so return 9 - input (replacing the digit with 9) minus input. |
| Input integer with leading zeros (e.g., 0123) | Convert the integer to a string and remove any leading zeros as the int itself will not contain leading zeros. |
| Integer where all digits are the same (e.g., 1111) | Replace all digits with 9 and then with 0 or 1 for min value according to rules. |
| Large integer that might cause integer overflow during calculations | Use long data type to store intermediate values and the result to prevent overflow. |
| An integer that already contains 0 or 9 | The algorithm should correctly handle digits of 0 or 9 present in the initial number. |
| Maximum integer value (Integer.MAX_VALUE) | Check if the replacement of any digit with 9 will cause an overflow when parsing the result to an integer, and use long if it does. |
| Number with only two distinct digits. | The algorithm will still successfully replace digits according to the defined rules to maximize and minimize values. |