Maximize Your Number: The "Maximum 69 Number" Challenge Explained!
Hey fellow coders! 👋 Vansh2710 here, ready to dive into another fun LeetCode problem that's perfect for beginners but offers a great lesson in greedy thinking. Today, we're tackling problem 1323: Maximum 69 Number.
Don't let the name scare you! This problem is quite approachable and teaches a fundamental concept in algorithm design. Let's break it down together!
🧐 Problem Explanation: What are we trying to do?
Imagine you have a positive integer, but here's the twist: it only consists of the digits 6 and 9. For example, 9669, 9996, or 66.
Your goal is to make this number as large as possible. You have one special power: you can change at most one digit. This means you can either:
- Change a
6to a9. - Change a
9to a6. - Or, choose not to change anything at all if it makes the number smaller.
After making your single optimal change (or no change), you need to return the largest possible number.
Let's look at an example:
If num = 9669:
- Change the first
9to6->6669 - Change the first
6to9->9969<-- This looks good! - Change the second
6to9->9699 - Change the last
9to6->9666
Comparing all these, 9969 is clearly the maximum.
See? Simple, right? Now, how do we find that "maximum" every time?
🤔 Intuition: The "Aha!" Moment
When you want to make a number as large as possible, what digits do you usually want in it? Larger digits! And where do those larger digits have the most impact? At the leftmost positions (highest place values).
Think about it:
- Changing
600to900increases the number by300. - Changing
006to009increases the number by3.
The digit furthest to the left (the "most significant" digit) contributes the most to the number's overall value.
So, if we have the power to change at most one digit, and we want to maximize the number, what's the best change we can make?
- We definitely don't want to change a
9to a6, because6is smaller than9, and that would always make our number smaller. - Therefore, our only useful change is to convert a
6into a9.
Now, if there are multiple 6s, which one should we change? To maximize the number, we should change the 6 that is furthest to the left. Why? Because changing 6 to 9 in the hundreds place (6xx to 9xx) makes a much bigger difference than changing it in the ones place (xx6 to xx9).
The "Aha!": To get the maximum number, find the first (leftmost) 6 and change it to a 9. If there are no 6s, the number is already all 9s, so no change is needed.
🪜 Approach: Step-by-Step Logic
Let's translate that intuition into a concrete plan:
- Convert to String: It's much easier to manipulate individual digits when the number is a string. If
numis an integer, convert it into a string or character array. For example,9669becomes"9669". - Iterate and Find: Go through the digits of the string from left to right (from the beginning of the string).
- The First '6' is Key: The very first time you find a character
'6', that's your target! Change that'6'to a'9'. - One Change Only: Since you can change at most one digit, once you've made this change, you're done! You've found the optimal modification. You can stop iterating.
- Convert Back: Convert the modified string back into an integer. This will be your maximum number.
- Edge Case (No '6's): What if you iterate through the entire string and never find a
'6'? This means the original number was already all9s (like9999). In this case, you wouldn't have made any changes, and the original number (now as a string, then converted back to int) is already the maximum.
💻 Code
Let's implement this in C. We'll use sprintf to convert the integer to a string, modify the string, and then atoi to convert it back.
#include <stdio.h> // Required for sprintf and printf (for testing)
#include <string.h> // Required for strlen
#include <stdlib.h> // Required for atoi
int maximum69Number(int num) {
// 1. Convert the integer to a string (character array).
// Constraints: 1 <= num <= 10^4.
// 10^4 has 5 digits. We need space for 5 digits + null terminator.
char num_str[6];
sprintf(num_str, "%d", num);
// 2. Iterate through the string to find the first '6'.
int len = strlen(num_str);
for (int i = 0; i < len; i++) {
// 3. If a '6' is found, change it to '9'.
if (num_str[i] == '6') {
num_str[i] = '9';
// 4. We can only make at most one change, so break after the first optimal change.
break;
}
}
// 5. Convert the modified string back to an integer and return.
return atoi(num_str);
}
⏱️ Time & Space Complexity Analysis
Let N be the number of digits in num. Given the constraint 1 <= num <= 10^4, N will be at most 5 digits.
-
Time Complexity:
-
sprintf(num_str, "%d", num): Converting an integer to a string takes time proportional to the number of digits,O(N). -
strlen(num_str): Calculating string length isO(N). - The
forloop iterates through the string at mostNtimes. In the worst case, we might iterate through all digits (if the '6' is the last digit, or if there are no '6's). So, this isO(N). -
atoi(num_str): Converting a string back to an integer also takes time proportional to its length,O(N). - Since
Nis very small and constant (at most 5), the overall time complexity is essentially O(1) (constant time).
-
-
Space Complexity:
- We create a character array
num_strto store the string representation ofnum. The size of this array is proportional to the number of digitsN. - Since
Nis constant (at most 5), the space complexity is also O(1) (constant space).
- We create a character array
This solution is highly efficient for the given constraints!
💡 Key Takeaways
- Greedy Approach: For maximization problems, a greedy strategy often works. Here, making the locally optimal choice (changing the leftmost
6to9) leads to the globally optimal solution. - Digit Manipulation: When you need to work with individual digits of a number, converting it to a string is a common and often convenient technique. Just remember to convert it back!
- Place Value Matters: Always remember how place value impacts a number's magnitude. Changes to digits on the left have a much greater effect.
I hope this explanation made the "Maximum 69 Number" problem clear and helped you understand the thought process behind solving it! Happy coding!
Authored by Vansh2710
Published: 2026-04-12 23:00:33
Top comments (0)