Maximum Swap - Problem

You're given an integer num, and you have exactly one opportunity to make a single swap between any two digits to create the largest possible number.

Goal: Maximize the value of the number by strategically swapping two digits at most once.

For example, if you have 2736, you could swap the 2 and 7 to get 7236, which is the maximum possible value with one swap.

Key Insight: You want to move the largest possible digit as far left as possible, while moving a smaller digit as far right as possible.

Input & Output

example_1.py โ€” Basic Swap
$ Input: num = 2736
โ€บ Output: 7236
๐Ÿ’ก Note: Swapping digits at positions 0 and 1 gives us the maximum number. We swap '2' and '7' to get 7236, which is the largest possible number with one swap.
example_2.py โ€” No Beneficial Swap
$ Input: num = 9973
โ€บ Output: 9973
๐Ÿ’ก Note: The number is already in descending order from left to right, so no single swap can make it larger. The function returns the original number.
example_3.py โ€” Multiple Same Digits
$ Input: num = 98368
โ€บ Output: 98863
๐Ÿ’ก Note: We swap the '3' at position 2 with the '8' at position 4 to get 98863. This gives us the maximum possible improvement by moving the largest available digit (8) to the leftmost position where it can make a difference.

Visualization

Tap to expand
Maximum Swap StrategyExample: 2736 โ†’ 72362Low value, left position7High value, right position36SWAP!7236Maximum Result: 7236Key Strategy1. Right-to-left scan: Track the maximum digit seen so far2. Identify opportunities: Mark positions where current < max_from_right3. Choose leftmost: The leftmost marked position maximizes value gain4. Single swap: Perform the most beneficial exchangeTime: O(n) | Space: O(n)
Understanding the Visualization
1
Identify the VIP
Scan from right to left to find the highest-value digit that could benefit from moving left
2
Find the Best Position
Locate the leftmost position where swapping would create maximum value increase
3
Execute the Swap
Perform the single most beneficial swap to maximize the number's value
Key Takeaway
๐ŸŽฏ Key Insight: By scanning right-to-left and tracking the maximum digit, we can identify the optimal swap position in a single pass, ensuring we move the highest-value digit to the leftmost position where it creates maximum impact.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the digit array from right to left

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Only need space for the digit array representation

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค num โ‰ค 108
  • The input number will have at most 8 digits
  • You can swap at most once - choose wisely!
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28
42.0K Views
Medium-High Frequency
~15 min Avg. Time
1.6K 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