# Next greater number on the basis of precedence of digits

Given a number **num** containing **n** digits. The problem is to find the next greater number using the same set of digits in **num** on the basis of the given precedence of digits. For example the precedence of digits is given as **1, 6, 4, 5, 2, 9, 8, 0, 7, 3** which simply means **1 < 6 < 4 < 5 < 2 < 9 < 8 < 0 < 7 < 3**. If next greater number cannot be formed then print the original number.

Examples:

Input : num = "231447" pre[] = {1, 6, 7, 5, 2, 9, 8, 0, 4, 3} Output : 237144 According to the precedence of digits1is being considered as the smallest digit and3is being considered as the largest digit. Input : num = "471" pre[] = {1, 6, 7, 5, 2, 9, 8, 0, 4, 3} Output : 471

**Approach:** Following are the steps:

- Create a
**priority[]**array of size ’10’. With the help of precedence array**pre[]**assign a priority number to each digit in**priority[]**where ‘1’ is being considered as smallest priority and ’10’ as the highest priority. - Using the STL C++ next_permutation with a manually defined compare function find the next greater permutation.

.

`// C++ implementation to find the next greater number ` `// on the basis of precedence of digits ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `#define DIGITS 10 ` ` ` `// priority[] to store the priority of digits ` `// on the basis of pre[] array. Here '1' is being ` `// considered as the smallest priority as '10' as ` `// the highest priority ` `int` `priority[DIGITS]; ` ` ` `// comparator function used for finding the ` `// the next greater permutation ` `struct` `compare { ` ` ` `bool` `operator()(` `char` `x, ` `char` `y) { ` ` ` `return` `priority[x - ` `'0'` `] < priority[y - ` `'0'` `]; ` ` ` `} ` `}; ` ` ` `// function to find the next greater number ` `// on the basis of precedence of digits ` `void` `nextGreater(` `char` `num[], ` `int` `n, ` `int` `pre[]) { ` ` ` `memset` `(priority, 0, ` `sizeof` `(priority)); ` ` ` ` ` `// variable to assgin priorities to digits ` ` ` `int` `assign = 1; ` ` ` ` ` `// assigning priorities to digits on ` ` ` `// the basis of pre[] ` ` ` `for` `(` `int` `i = 0; i < DIGITS; i++) { ` ` ` `priority[pre[i]] = assign; ` ` ` `assign++; ` ` ` `} ` ` ` ` ` `// find the next greater permutation of 'num' ` ` ` `// using the compare() function ` ` ` `bool` `a = next_permutation(num, num + n, compare()); ` ` ` ` ` `// if the next greater permutation does not exists ` ` ` `// then store the original number back to 'num' ` ` ` `// using 'pre_permutation'. ` ` ` `if` `(a == ` `false` `) ` ` ` `prev_permutation(num, num + n, compare()); ` `} ` ` ` `// Driver program to test above ` `int` `main() { ` ` ` `char` `num[] = ` `"231447"` `; ` ` ` `int` `n = ` `strlen` `(num); ` ` ` `int` `pre[] = {1, 6, 7, 5, 2, 9, 8, 0, 4, 3}; ` ` ` `nextGreater(num, n, pre); ` ` ` `cout << ` `"Next Greater: "` `<< num; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Next Greater: 237144

Time Complexity: O(n).

Auxiliary Space: O(1).

## Recommended Posts:

- Find next greater number with same set of digits
- Largest number not greater than N all the digits of which are odd
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Smallest number with given sum of digits and sum of square of digits
- Number of digits in the nth number made of given four digits
- Least Greater number with same digit sum
- Largest even digit number not greater than N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find count of digits in a number that divide the number
- Find maximum number that can be formed using digits of a given number
- Build Lowest Number by Removing n digits from a given number
- Find the smallest number whose digits multiply to a given number n

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.