Mid/SeniorGreedyMath

Maximum Swap

You are given an integer num. You can swap two digits at most once to get the maximum valued number.

Return the maximum valued number you can get.

Example 1
Inputnum = 2736
Output7236
Swap the number 2 and the number 7.
Example 2
Inputnum = 9973
Output9973
No swap is needed because the number is already maximized.

Constraints

  • 0 <= num <= 10^8

Asked at 8 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate