Given a number, find the next higher number which has the exact same set of digits as the original number
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In the realm of computational challenges and algorithmic puzzles, one interesting problem is determining the next higher number that uses the exact same digits as a given number. This problem has applications in permutations and combinations, and solving it involves understanding number manipulation, sorting, and logic.
The Problem Statement
Given a number, our task is to find the next higher number which has the exact same set of digits as the original number. If no such higher number exists, we return an indication that it is not possible.
Example
Consider the number 534976. The next higher number that preserves the use of the same digits is 536479.
Technical Approach
To solve this problem, follow these steps:
- Identify Pivot: Traverse the number from right to left to find the first digit that is smaller than the digit next to it. This is known as the "pivot."
- Find Successor: Next, find the smallest digit on the right side of the pivot that is larger than the pivot digit.
- Swap the Pivot and Successor: Swap these two digits.
- Sort the Suffix: Finally, sort the digits to the right of the pivot to obtain the smallest sequence of digits. Since the elements were initially from left to right, this corresponds to reversing the sequence post-swap.
Example Walkthrough
Let's apply our method to the number 534976:
- Identifying the Pivot: Starting from the end, we find that
9is the first number larger than7(not a candidate), then4, which is smaller than9, becomes our pivot. - Find Successor: The smallest number larger than
4on its right side is6. - Swap Pivot and Successor: Swap
4and6to get536974. - Sort the Suffix: Sorting
974yields479.
Thus, the next higher number with the exact same set of digits as 534976 is 536479.
Algorithm
Here's a concise version of the algorithm in pseudocode:
Time Complexity
The time complexity of this algorithm is , where is the number of digits in the number. This is because the search for the pivot, the search for the successor, and the reverse operation all take linear time.
Summary Table
| Task | Explanation |
| Identify Pivot | Search from right to left for the first digit smaller than the next digit. |
| Find Successor | Identify the smallest digit larger than the pivot to its right. |
| Swap Pivot and Successor | Swap these identified digits. |
| Sort the Suffix | Reverse the digits right of the pivot to ensure the smallest sequence. |
Conclusion
Solving the problem of finding the next higher number with the exact same set of digits involves a clever blend of sequence identification and sorting. Understanding this approach not only enhances problem-solving skills but also deepens one's grasp of permutations. This method ensures efficiency and clarity in finding the desired solution.
Related reading
- Given a permutation''s lexicographic number, is it possible to get any item in it in O1
- Given a set of points, how do I find the two points that are farthest from each other?
- Given a set of rectangles, do any overlap?
- Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate?
- given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted array
- Given boundaries, find interval
- Given n and k, return the kth permutation sequence
- Given parallel lists, how can I sort one while permuting rearranging the other in the same way?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.