Next Greater Even Number | Practice | GeeksforGeeks

Given a positive integer X. The task is to find the smallest even number E such that
E > X and all digits in X and E are same.
Note: All the digits in X should be same with digits in E.
 
Example 1:
Input:
X = 34722641
Output:
34724126
Explanation:
Next greater number with same 
digits as in input is 34724126.

Example 2:
Input:
X = 111
Output:
-1
Explanation:
You can't rearrange the digits to get an answer.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function leftIndex() which takes a string X as inputs, which is representing the number and returns the required smallest even number. If no such even number exists return -1.

Expected Time Complexity: O(Log(N)! )
Expected Auxiliary Space: O(Log(N))

Constraints:
1 ≤ N ≤ 109
0
ghosh15subhradeep2 weeks ago
class Solution{   public:   long long getNextEven(string x)   {       // Your code goes here       int n=x.size();       string s=x;       sort(s.begin(),s.end());       do{           next_permutation(x.begin(),x.end());       }while((x[n-1]-'0')%2!=0&&x!=s);       if(x==s)           return -1;       return stoll(x);   }};
+2
chessnoobdj4 months ago
C++ STL
long long getNextEven(string X)
{
    string E = X;
    while(next_permutation(E.begin(), E.end())){
        if((E.back()-'0')%2 == 0)
            return stoll(E);
    }
    return -1;
}
0
saket727404 months ago
public long getNextEven(String x)
   {
       // Your code goes here
       while(true){
           int n = x.length();
           int idx = -1;
           
           for(int i=1;i<n;i++){
               if(x.charAt(i) > x.charAt(i-1))
                   idx = i;
           }
           
           if(idx == -1)
               return -1l;
           
           int index = idx;
           for(int i=idx;i<x.length();i++){
               if(x.charAt(i) > x.charAt(idx-1) && x.charAt(i) < x.charAt(idx))
                   index = i;
           }
           
           char arr[] = x.toCharArray();
           
           char temp = arr[idx-1];
           arr[idx-1] = arr[index];
           arr[index] = temp;
           
           Arrays.sort(arr,idx,n);
           
           if((arr[n-1]-'0')%2 == 0){
               String s = String.valueOf(arr);
               return Long.parseLong(s);
           }
       }
   }
// can someone tell where to optimise in my java code, showing runtime error.
+3
ameenul88087 months ago
long long getNextEven(string x)
    {
        // Your code goes here
        while(true){
            int idx=-1;
            
            //finding the last peak element 
            
            for(int i=1;i<x.length();i++){
                if(x[i]>x[i-1]) idx=i;
            }
            
            //if there is no peak element i.e if the string is in descending order
            if(idx==-1){
                return (long long)-1;
            }
            
            int index = idx;
            
            //we have to swapp the peak element with its prevoius element 
            //if there exist a number after peak element which is smaller than peak but 
            //greater than element before peak element 
            //then we have to swap those two 
            //so that we can find the immediate greater instead of some random greater no
            //swapping lower place digits result in next greater 
            
            for(int i=idx;i<x.length();i++){
                if(x[i]>x[idx-1] && x[i]<x[idx]){
                    index = i;
                }
            }
            
            swap(x[idx-1],x[index]);
            
            //sort the remaining digits in ascending 
            sort(x.begin()+idx,x.end());
            
            
            //if its an even number then return 
            //else find next greater permutation from this number 
            //if no even numbers are there in array or if no greater even permutation is found
            //eventually there will be a permuatation in descending order 
            //then the loop will returrn -1
            if(x[x.length()-1]%2==0){
                return stoll(x);
            }
        }
    }
0
anutiger7 months ago
        long long cur = stoll(s);
        while(next_permutation(s.begin(),s.end())){
            int t = s[s.length() - 1] - '0';
            if(t % 2 == 0 and stoll(s) > cur)
                return stoll(s);
        }
        return -1;
+3
Shreyansh Kumar Singh9 months ago
Shreyansh Kumar Singh
This doesn't seems to be an easy question!
0
Debojyoti Sinha10 months ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.55
class Solution{    public:    long long getNextEven(string x)    {        long long prevX = stoll(x);                next_permutation(x.begin(), x.end());                do        {            int lastDigit = x[x.size() - 1] - '0';                        if(lastDigit % 2 == 0)            {                long long currX = stoll(x);                                return currX > prevX ? currX : -1;            }        }        while(next_permutation(x.begin(), x.end()));                return -1;    }};
0
Nikhil Pratik
This comment was deleted.
0
Sathish Kumar2 years ago
Sathish Kumar
Can anyone say why in the editorial section they provided the answer for the next largest palindrome? Is there any relation for this problem?
0
mayank aggarwal2 years ago
mayank aggarwal
Using next_permutationhttps://ide.geeksforgeeks.o...
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.