Maximize an Integer with at Most One Digit Swap

Quick Overview

Maximize an Integer with at Most One Digit Swap. Return the greatest integer obtainable from num by swapping two decimal digit positions at most once.

Maximize an Integer with at Most One Digit Swap

Company: Nielsen

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the greatest integer obtainable from num by swapping two decimal digit positions at most once. Leaving the digits unchanged is allowed. ```cpp class Solution { public: int maximumSwap(int num); }; ``` ### Examples **Example 1:** ```text num = 1993 Output: 9913 Explanation: Swap the first 1 with the rightmost 9. ``` **Example 2:** ```text num = 4321 Output: 4321 Explanation: No swap improves the value. ``` ### Constraints - The integer num is nonnegative and at most 100000000. ### Clarifying Questions Prioritize the earliest improved digit and allow no swap when already maximal. ```hint Read the boundary condition If the largest useful replacement digit appears twice, which occurrence should receive the smaller digit? ``` ### Contract considerations Prioritize the earliest improved digit and allow no swap when already maximal.

Overview: Maximize an Integer with at Most One Digit Swap. Return the greatest integer obtainable from num by swapping two decimal digit positions at most once.

Nielsen logo
Nielsen
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the greatest integer obtainable from num by swapping two decimal digit positions at most once. Leaving the digits unchanged is allowed.

class Solution {
public:
    int maximumSwap(int num);
};

Examples

Example 1:

num = 1993
Output: 9913
Explanation: Swap the first 1 with the rightmost 9.

Example 2:

num = 4321
Output: 4321
Explanation: No swap improves the value.

Constraints

  • The integer num is nonnegative and at most 100000000.

Clarifying Questions Guidance

Prioritize the earliest improved digit and allow no swap when already maximal.

Contract considerations

Prioritize the earliest improved digit and allow no swap when already maximal.