algorithms
unique numbers
digit manipulation
permutation
number theory

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:

  1. Scan from right to left to find the first digit smaller than the digit after it.
  2. Find the smallest digit to the right that is greater than the pivot.
  3. Swap those two digits.
  4. 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, because 1 is smaller than 8.
  • The smallest digit greater than 1 in the suffix is 5.
  • 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.

python
1def next_higher_number(n):
2    digits = list(str(n))
3
4    pivot = len(digits) - 2
5    while pivot >= 0 and digits[pivot] >= digits[pivot + 1]:
6        pivot -= 1
7
8    if pivot < 0:
9        return None
10
11    successor = len(digits) - 1
12    while digits[successor] <= digits[pivot]:
13        successor -= 1
14
15    digits[pivot], digits[successor] = digits[successor], digits[pivot]
16    digits[pivot + 1:] = reversed(digits[pivot + 1:])
17
18    return int("".join(digits))
19
20
21print(next_higher_number(218765))
22print(next_higher_number(534976))
23print(next_higher_number(987654))

Output:

text
251678
536479
None

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.

python
print(next_higher_number(1223))

Output:

text
1232

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.
  • 'None or a similar sentinel value is useful when no larger permutation exists.'
  • The same logic works for repeated digits as well as distinct ones.

Course illustration
Course illustration

All Rights Reserved.