Next greater number from the same set of digits in Python

In this tutorial, we are here to find the next greater number from the same set of digits in Python entered by the user. Usually many times the reader gets often confused that what the question is actually wanting us to find/discover. This can be easily understood with the help of the few examples described below in this tutorial. So now let’s understand the question with some example.

Next greater number from the same set of digits in Python

Let us first understand the question in detail what it is trying to say. Let me make it clear first that you do not have to find the 2nd largest among the list/array entered by the user, that is too easy. To understand the question go through the example and explanation below:-

Suppose the user enters 2510 now the next greatest number would be 5012. Thus, we have to find the number such that it is the largest number using the same digits that are entered by the user.

Suppose you have got a number 1243
Now you have to find the next greater number using the same digits as the previous number. that means you can use only “one two four and three” these digits only.
So the next greater number will be “1423”
Because “1234” is smaller than 1243
And 1432 will be higher than 1423

Example 1:

Input:1243

Output:1324

Let us take another example:

Input:2540

Output:4025

You may also read:

Algorithm:

  1. Input the number.
  2. Produce all possible combinations of the number’s digit.
  3. Now sort the number.
  4. Find the index of the number input by the user in the sorted list and add 1 to it to get the index of the second largest number and print the same.

To produce the combination of the digits of the number use the python3 in-built module. In Python3 there are methods directly that can be used to produce all permutations and combinations from the given set of digits. That module can be imported and used by us as follows:

from itertools import permutations

Before looking at the code below try to code it yourself as per the algorithm and the explanation provided above. If you have any queries type in the comment section. Keep going, coders!! Cheers.

Code in Python3:

from itertools import permutations
number=input()
choices=list(map(int,number)) #split the number and convert it to integer list of digits
res=permutations(number)      #produce all combinations of the digits
res2=[]                          
for i in res:
    res2.append(int(''.join(i)))     
res2=sorted(res2)
print(res2[res2.index(int(number))+1])  #print second largest digit

The sorted function takes O(nlog n) time complexity.

One response to “Next greater number from the same set of digits in Python”

  1. Abhishek Gupta says:

    Equivalent Java Program:

    import java.util.*;
    public class HelloWorld {
    ArrayList arr = new ArrayList();
    public static void main(String...args){
    int i;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number for finding next greatest number");
    String s= input.next();
    int array[]= new int[s.length()];
    HelloWorld a = new HelloWorld();
    a.permute(s,0,s.length()-1);
    System.out.println("All Permutations of digits of the number:");
    for(i=0;i<a.arr.size();i++)
    System.out.println(a.arr.get(i));
    Collections.sort(a.arr);
    System.out.println("Greatest Next Number of the given number:");
    System.out.println(a.arr.get(a.arr.indexOf(Integer.parseInt(s))+1));
    }
    void permute(String arr,int low,int high) {
    String str=arr;
    if (low!=high){
    for(int i=0;i<=high-low;i++){
    str=this.swap(arr,low,low+i);
    this.permute(str,low+1,high);
    }
    }
    else if(low==high) this.arr.add(Integer.parseInt(str));

    }
    String swap(String s,int a, int b){
    char arr[]=s.toCharArray();
    char temp;
    temp=arr[a];
    arr[a]=arr[b];
    arr[b]=temp;
    return String.valueOf(arr);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *