Next greater number on the basis of the precedence of digits


In the normal number system, 0 is the smallest digit while 9 is the largest digit. In this problem, we will be given a list of the length 10, and starting from index 0 to index 9 it represents a digit, which indicates the priority of that digit and the list will be in increasing order means the digit present at the last index is the with highest priority. We will be given a number also and we have to find the next number which is the just greater than the current number.

Input 1: 
Given number = “123”
Given array = { 9, 2, 3, 6, 8, 1, 0, 5, 4, 7}
Output: 132

Explanation

From the given priority array, we can see that the priority of the 1 is greater than both 2 and 3. Priority of 3 is greater as compared to of 2. So, if the next permutation of the given number will be achieved by swapping 2 and 3.

Input 2:
Given number = 132
Given array = { 9, 2, 3, 6, 8, 1, 0, 5, 4, 7}
Output: 132

Explanation

From the given priority array, we can see that the priority of 1 is greater than both 2 and 3. Priority of 3 is greater as compared to of 2. So, there will be no next permutation available.

Approach

In this approach, we will use the concept of the standard template library (STL) provided by the C++ programming language to get the next permutation. Let us see the steps −

  • First, we will be given the number to the next number from which, and an array indicating the priority of the digits in the increasing order.

  • We will call to the predefined function to find the next number that is greater as compared to the current given number.

  • We will define a function that take the parameter given number and array.

  • We will define a global array and store the priority of each digit taken from the given array to use later.

  • We will convert the given number to a string to apply the next permutation function on it.

  • We will define a custom function that will used to compare the characters of the string in the next permutation function of the stl.

  • After getting the next permutation we will convert string to integer and will return it.

Example

#include <bits/stdc++.h>
using namespace std;
// priority array or array in which the priority of each digit will be stored 
int prio[10];
// defining the compare function 
bool compare(char& a, char& b){
   return prio[a-'0'] < prio[b-'0'];
}
// function to get the next permuatation 
int nextNum(int n, int arr[]){
   for(int i=0; i<10; i++){
      prio[arr[i]] = i;
   }
   // converting the given number into string 
   string str = to_string(n);
   // calling the next permuatation stl function for the given string we will compare by custom function 
   bool cur = next_permutation(str.begin(),str.end(),compare);
   if(cur == false){
      // indicating the next permutation does not exist 
      return n;
   }
   n = stoi(str); // converting string back to number 
   return n;
}
int main(){
   int n = 312; // the given number 
   int arr[] = {9, 2, 3, 6, 8, 1, 0, 5, 4, 7}; // given array
   cout<<"The next number or permutation for the given number is: "<<nextNum(n, arr);
   return 0;
}

Output

The next number or permutation for the given number is: 123

Time and Space Complexity

The time complexity of the above code is O(N), where N is the number of digits in the given number.

The space complexity of the above code is O(N) because we are creating a new string to use the next permutation function.

Note: If the current number is the greater number among all the permutations then the next permutation function will return false and we will return the same number as the next permutation does not exist.

Conclusion

In this tutorial, we have implemented a code to find the next number which is the just greater than the current number but the priority of the digits will be not in the same order that it is from 0 to 9, instead it will be given in the separate array. We have used the STL function next permutation and a custom function to get the next number based on the new priority. The time and space complexity of the above code O(Number of digits).

Updated on: 16-May-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements