Skip to content
Related Articles

Related Articles

Improve Article
Next greater number on the basis of precedence of digits
  • Difficulty Level : Expert
  • Last Updated : 18 Feb, 2018

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.

    .






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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :