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 digits 1 is 
being considered as the smallest digit and 3
is 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:

  1. 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.
  2. Using the STL C++ next_permutation with a manually defined compare function find the next greater permutation.
  3. .

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

Next Greater: 237144

Time Complexity: O(n).
Auxiliary Space: O(1).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.