Next higher number
Digit permutation
Number transformation
Algorithmic problem
Mathematical challenge

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.

Practice algorithms

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:

  1. 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."
  2. Find Successor: Next, find the smallest digit on the right side of the pivot that is larger than the pivot digit.
  3. Swap the Pivot and Successor: Swap these two digits.
  4. 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:

  1. Identifying the Pivot: Starting from the end, we find that 9 is the first number larger than 7 (not a candidate), then 4, which is smaller than 9, becomes our pivot.
  2. Find Successor: The smallest number larger than 4 on its right side is 6.
  3. Swap Pivot and Successor: Swap 4 and 6 to get 536974.
  4. Sort the Suffix: Sorting 974 yields 479.

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:

 
1function nextPermutation(num):
2    num_list = convert num to array of digits
3
4    # Step 1: Find pivot
5    for i from end of num_list to 1:
6        if num_list[i-1] < num_list[i]:
7            break
8
9    if no pivot found:
10        return "Not possible"
11
12    # Step 2: Find successor
13    for j from end of num_list:
14        if num_list[j] > num_list[i-1]:
15            break
16
17    # Step 3: Swap
18    swap(num_list[i-1], num_list[j])
19
20    # Step 4: Sort the suffix
21    reverse(num_list[i to end])
22
23    return convert num_list back to number

Time Complexity

The time complexity of this algorithm is O(n)O(n), where nn 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

TaskExplanation
Identify PivotSearch from right to left for the first digit smaller than the next digit.
Find SuccessorIdentify the smallest digit larger than the pivot to its right.
Swap Pivot and SuccessorSwap these identified digits.
Sort the SuffixReverse 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms