Find next highest unique number from the given digits
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The next highest number that can be formed from a given set of digits is the next lexicographic permutation of those digits. The task is not to generate every permutation and sort them, but to make one targeted change that produces the smallest number strictly larger than the current one.
The Core Idea Is Next Permutation
Suppose the number is 218765. Reading from right to left, you look for the first position where the digits stop descending. That position is the pivot, because changing anything to its right alone cannot make the number larger in the smallest possible way.
The algorithm is:
- Scan from right to left to find the first digit smaller than the digit after it.
- Find the smallest digit to the right that is greater than the pivot.
- Swap those two digits.
- Reverse the suffix to the right of the pivot.
That final reversal matters because the suffix is already in descending order, so reversing it gives the smallest possible tail.
Walk Through an Example
Take 218765.
- From the right, the descending suffix is
8765. - The pivot is
1, because1is smaller than8. - The smallest digit greater than
1in the suffix is5. - Swap to get
258761. - Reverse the suffix after the pivot position to get
251678.
251678 is the next higher number using the same digits.
Python Implementation
A direct implementation can operate on a list of characters or digits.
Output:
Returning None is a practical way to signal that no larger arrangement exists. For 987654, the digits are already in descending order, so the input is the largest possible permutation.
Why This Is Better Than Brute Force
A brute-force solution would generate all permutations, filter those greater than the original value, and select the smallest. That approach is far too expensive once the number of digits grows, because permutations grow factorially.
The next-permutation algorithm is linear in the number of digits, which makes it the correct tool for this problem.
Repeated Digits Still Work
The title often says “unique number,” but the algorithm also works when digits repeat. What matters is that the result uses the same multiset of digits and is strictly greater than the original number.
Output:
The pivot and successor logic still holds because lexicographic ordering works the same way with repeated elements.
String Input Can Be Safer Than Integer Math
Working with the digits as a string is simpler than repeatedly taking remainders with division. It avoids mistakes with leading zeroes during intermediate steps and keeps the code close to the actual permutation logic.
If you need to process very large values that do not fit comfortably in a fixed-width integer in another language, string-based processing is the standard approach.
Common Pitfalls
- Generating every permutation instead of using the linear next-permutation algorithm.
- Swapping with a greater digit but forgetting to reorder the suffix into its smallest form.
- Returning the next greater digit swap result without reversing the descending tail.
- Failing to handle the case where the digits are already in descending order.
- Assuming the method only works when every digit is distinct.
Summary
- The problem is the next lexicographic permutation of the digit sequence.
- Find a pivot from the right, swap with the smallest greater successor, then reverse the suffix.
- The algorithm runs in linear time relative to the number of digits.
- '
Noneor a similar sentinel value is useful when no larger permutation exists.' - The same logic works for repeated digits as well as distinct ones.

