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).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find next greater number with same set of digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Next higher palindromic number using the same set of digits
- Next Number with distinct digits
- 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
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Largest number not greater than N all the digits of which are odd
- Nearest greater number by interchanging the digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number greater than Y with sum of digits equal to X
- Smallest number with given sum of digits and sum of square of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits

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.