Find the Next Larger Integer with the Same Digits

Find the Next Larger Integer with the Same Digits

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Rearrange the decimal digits of positive n to obtain the smallest integer strictly larger than n. Return -1 if none exists or if the smallest larger arrangement exceeds signed 32-bit int. ```cpp class Solution { public: int nextGreaterElement(int n); }; ``` ### Examples **Example 1:** ```text n = 1243 Output: 1324 Explanation: No smaller rearrangement greater than 1243 exists. ``` **Example 2:** ```text n = 543 Output: -1 Explanation: Its digits already form the largest arrangement. ``` ### Constraints - The positive input n is at most 2147483647. ### Clarifying Questions Preserve digit multiplicities and check the explicit signed-32-bit output boundary. ```hint Read the boundary condition Which rightmost digit can be increased while changing as little of the prefix as possible? ``` ### Contract considerations Preserve digit multiplicities and check the explicit signed-32-bit output boundary.
Bloomberg logo
Bloomberg
Jul 7, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Rearrange the decimal digits of positive n to obtain the smallest integer strictly larger than n. Return -1 if none exists or if the smallest larger arrangement exceeds signed 32-bit int.

class Solution {
public:
    int nextGreaterElement(int n);
};

Examples

Example 1:

n = 1243
Output: 1324
Explanation: No smaller rearrangement greater than 1243 exists.

Example 2:

n = 543
Output: -1
Explanation: Its digits already form the largest arrangement.

Constraints

  • The positive input n is at most 2147483647.

Clarifying Questions Guidance

Preserve digit multiplicities and check the explicit signed-32-bit output boundary.

Contract considerations

Preserve digit multiplicities and check the explicit signed-32-bit output boundary.