Maximum 69 Number - Problem

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).

Input & Output

Example 1 — Basic Case with 6
$ Input: num = 9669
Output: 9969
💡 Note: Change the first 6 to 9: 9669 → 9969. This gives maximum value since 6→9 increases the hundreds place.
Example 2 — All 9s
$ Input: num = 9996
Output: 9999
💡 Note: Change the last 6 to 9: 9996 → 9999. Only one 6 present, changing it maximizes the number.
Example 3 — No 6s present
$ Input: num = 9999
Output: 9996
💡 Note: No 6s to change to 9s. Change the rightmost 9 to 6: 9999 → 9996 for maximum with constraint.

Constraints

  • 1 ≤ num ≤ 104
  • num consists only of 6 and 9 digits

Visualization

Tap to expand
Maximum 69 Number: Transform to Get Maximum ValueINPUT9669PROCESSFind first 6 from leftChange 6 → 9 for maximumimpact on place valueOUTPUT9969Original: 9669Maximum: 99699669First 6 found!Greedy Strategy: Maximum impact by changing leftmost 6 to 9
Understanding the Visualization
1
Input
Given number with only 6s and 9s
2
Strategy
Find leftmost 6 and change to 9
3
Output
Maximum possible number
Key Takeaway
🎯 Key Insight: The leftmost digit has the highest place value, so changing the first 6 to 9 maximizes the result
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen