Next greater number set digits | Practice | GeeksforGeeks

Given a number n, find the smallest number that has same set of digits as n and is greater than n. If n is the greatest possible number with its set of digits, report it.
Example 1:
Input:
N = 143
Output: 314
Explanation: Numbers possible with digits 
1, 3 and 4 are: 134, 143, 314, 341, 413, 431.
The first greater number after 143 is 314.


â€‹Example 2:
Input: 
N = 431
Output: not possible
Explanation: Numbers possible with digits
1, 3 and 4 are: 134, 143, 314, 341, 413, 431.
Clearly, there's no number greater than 431.


Your Task:
You don't need to read input or print anything. Your task is to complete the function findNext () which takes an integer N as input and returns the smallest number greater than N with the same set of digits as N. If such a number is not possible, return -1.

Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(LogN).

Constraints:
1 ≤ N ≤ 100000
0
gaurabhkumarjha271020013 months ago
 string s= to_string (N);// integer N to string s
 int temp;
        
   while (next_permutation (s.begin(), s.end())){
            
 temp= stoi(s);// convert string s to integer temp use stoi
            
            if (temp > N)
             return temp;
        }
        
        return -1;
0
apoorvmishra4 months ago
void swap(char *a, char *b){   char temp = *a;   *a = *b;   *b = temp;}   int findNext (int N)    {       string arr=to_string(N);       int i;       for(i=arr.size()-1;i>=0;i--){           //cout<<arr[i];           if(arr[i]>arr[i-1]) break;       }       //cout<<i<<endl;       if(i==0) return -1;       int ind=i;       for(int j=arr.size()-1;j>i;j--){           if(arr[j]>arr[i-1]){               ind=j;               break;           }       }       //cout<<ind<<endl;       swap(&arr[i-1],&arr[ind]);       sort(arr.begin()+i,arr.end());       return stoi(arr);   }
0
chessnoobdj4 months ago
Easy c++
int findNext (int N) 
    {
        string str = to_string(N);
        if(!next_permutation(str.begin(), str.end()))
            return -1;
        return stoi(str);
    }  
0
sureshrao10000015 months ago
class Solution{public:int findNext(int n) {    if(n == 1) return -1;    string num = to_string(n);    int i, j;    for(i = num.length()-1; i > 0; i--){        if(num[i] > num[i-1]) break;    }    if(i == 0) return -1;    for(j = num.length()-1; j >= i; j--){        if(num[i-1] < num[j]) {      swap(num[i-1], num[j]);         break;        }    }    reverse(num.begin()+i, num.end());    return stoi(num);}};
0
adnansheikh5 months ago
.
0
yoursnataraj6 months ago
    string s = to_string(N);
    for(int i=1;i<s.length();i++)
    {
        if(s[i-1]>=s[i])
        {
            if(i+1==s.length()) return -1;
        }
        else
        {
            break;
        }
    }
    stack<int> st;
    for(int i=s.length()-1;i>=0;i--)
    {
        if(s[i-1]>=s[i])
        {
            st.push(i);
        }
        else
        {
            st.push(i);
            int t;
            while(!st.empty()&&s[i-1]<s[st.top()])
            {
                t=st.top();
                st.pop();
            }
            swap(s[i-1],s[t]);
            reverse(s.begin()+i,s.end());
            break;
        }
    }
    return stoi(s);
https://youtu.be/Nbi8lK80Rqk
0
chamoliabhishek0077 months ago
from itertools import permutationsclass Solution:   def findNext (self,n):       # your code here       k=str(n)       permutation = [''.join(p) for p in permutations(k)]       p=permutation       p=list(sorted(set(p)))              if (p[len(p)-1])>k:           return p[p.index(k)+1]       return -1#simple           
0
Sumit Rathore10 months ago
Sumit Rathore
o(log(n)) solutionhttps://uploads.disquscdn.c...
0
Drigger1 year ago
Drigger
for reference: https://www.geeksforgeeks.o...
0
Ankush Singh1 year ago
Ankush Singh
No doubt it is not an easy level question
int findNext (int N)     {        //code here.        string s=to_string(N);int t;        while(next_permutation(s.begin(),s.end()))        {            t=stoi(s);            if(t>N)                return t;        }         return -1;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.