You are given a positive integer num consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
Example 1:
Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in 9699. Changing the fourth digit results in 9666. The maximum number is 9969.
Example 2:
Input: num = 9996 Output: 9999 Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:
Input: num = 9999 Output: 9999 Explanation: It is better not to apply any change.
Constraints:
1 <= num <= 104num consists of only 6 and 9 digits.When 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 idea here is to try making the number as large as possible by changing a 6 to a 9. The brute force way is to consider every single place you could possibly make that change and see which change results in the biggest number.
Here's how the algorithm would work step-by-step:
def maximum_69_number(number_to_process):
string_representation_of_number = str(number_to_process)
maximum_possible_number = number_to_process
# Iterate through each digit to explore replacement possibilities
for digit_index in range(len(string_representation_of_number)):
# Only consider replacing a '6' to maximize the number
if string_representation_of_number[digit_index] == '6':
# Create a new string by replacing the '6' at the current index
modified_number_string = list(string_representation_of_number)
modified_number_string[digit_index] = '9'
new_number = int("".join(modified_number_string))
# Update the maximum if the newly formed number is larger
if new_number > maximum_possible_number:
maximum_possible_number = new_number
return maximum_possible_numberThe goal is to make the largest possible number by changing at most one digit. The key is to find the first '6' from the left and change it to a '9' to maximize the value.
Here's how the algorithm would work step-by-step:
def maximum_six_nine(number_input):
digits_of_number = list(str(number_input))
found_six = False
# Iterate from left to right to find the first '6'.
for index, digit in enumerate(digits_of_number):
# Changing the first '6' encountered maximizes the number.
if digit == '6' and not found_six:
digits_of_number[index] = '9'
found_six = True
# Once the change is made, stop to ensure only one digit is altered.
break
# Reconstruct the number from the modified digits.
return int("".join(digits_of_number))| Case | How to Handle |
|---|---|
| Input string is empty or null | The problem statement specifies a positive integer, so empty or null inputs are not expected; robust code might return an empty string or throw an error. |
| Input string contains only 9s | No 6s are present, so no change can be made; the original string should be returned. |
| Input string contains only 6s | The leftmost 6 should be changed to a 9 to maximize the number; this involves changing the first character. |
| Input string has a single digit | If the digit is 6, change it to 9; if it's 9, return it as is. |
| Input string contains mixed 6s and 9s | Iterate from left to right and change the first encountered 6 to a 9. |
| Very long input string | The solution should scale linearly with the length of the string, which is efficient for typical interview constraints. |
| Input string contains digits other than 6 or 9 | The problem statement guarantees only 6s and 9s; unexpected characters would require validation or error handling. |
| Multiple 6s exist, which 6 to change? | To maximize the number, change the leftmost 6 to a 9, as this has the greatest positional value. |