Largest Even Number | Practice | GeeksforGeeks

Given an integer S. Find the largest even number that can be formed by rearranging the digits of S.
Note: In case the number does not contain any even digit then output the largest odd number possible.
Example 1:
Input:
S = "1324"
Output: "4312"
Explanation: Largest even number: 4312

Example 2:
Input:
S = "3555"
Output: "5553"
Explanation: No even number possible,
So we'll find largest odd number.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function LargestEven() which takes the string S as inputs representing the integer and returns the answer.

Expected Time Complexity: O(|S| * log |S|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |S| ≤ 105
S contains only digits from '0' to '9'.
0
cedera987et2 days ago
Python Solution:
 
 
 
 
class Solution:   def LargestEven(self, S):              a = list(S)       e = []       o = []       for i in range(len(a)):           if int(a[i])%2==0:               e.append(int(a[i]))           else:                o.append(int(a[i]))              if len(e)==0:           return "".join(sorted(a, reverse=True))              e.sort()       t = e[0]       e.pop(0)              c = e+o       c.sort(reverse=True)       c.append(t)       for i in range(len(c)):           c[i] = str(c[i])       return "".join(c)               
+1
omkarsubhasishkhuntia5 months ago
 vector<int>v;       for(int i=0;i<S.size();i++){           v.push_back(int(S[i]));       }       sort(v.begin(),v.end());       reverse(v.begin(),v.end());       for(int i=S.size()-1;i>=0;i--){           if(v[i]%2==0){               while(i!=S.size()-1){                   swap(v[i],v[i+1]);                   i++;               }               break;           }       }         string res="";       for(int i=0;i<S.size();i++){           res+=char(v[i]);       }       return res;
0
pikucode156 months ago
class Solution{   public:   string LargestEven(string S){       sort(S.begin(),S.end(),greater<char>());       int evMinIndx=-1;       for(int i=S.size()-1;i>=0;i--)       {           if((S[i]-'0')%2==0)           {               evMinIndx=i;               break;           }       }       if(evMinIndx==-1)           return S;       char temp=S[evMinIndx];       for(int i=evMinIndx;i<S.size()-1;i++)           S[i]=S[i+1];       S[S.size()-1]=temp;       return S;   }  };
-1
sknwd88647 months ago
string LargestEven(string str){
         sort(str.begin(),str.end(),greater<int>());
         int len=str.length();
         for(int i=len-1;i>=0;i--)
         {
             if((int)str[i]%2==0)
             {
                 swap(str[i],str[len-1]);
                 break;
             }
         }
         sort(str.begin(),str.begin()+len-1,greater<int>());
         return str;
    }  
0
Faisal Alam9 months ago
Faisal Alam
O(nlogn) O(1)class Solution{    public:    string LargestEven(string str){        char num;        int index=-1;        sort(str.begin(), str.end(), greater<int>());        for(int i=str.size()-1; i>=0; i--){            if(str[i]%2==0){    //ascii values of even char number is also even so it works without conversion                index=i;                num=str[index];                break;            }        }        if(index!=-1){            for(int i=index+1; i<str.size(); i++){="" str[i-1]="str[i];" }="" str[str.size()-1]="num;" }="" return="" str;="" }="" };="" <="" code="">
0
Jashwant Kumar9 months ago
Jashwant Kumar
int res[10];        bool even=false;        for(int i=0;i<10;i++)        {            res[i]=0;        }        int n=S.length();        for(int i=0;i<n;i++) {="" char="" s1="S[i];" res[s1-'0']++;="" }="" for(int="" i="0;i&lt;9;i=i+2)" {="" if(res[i]="">0)            {                res[i]--;                S[n-1]='0'+i;                even=true;                break;            }        }        if(even==false)        {            sort(S.begin(),S.end(),greater<>());            return S;        }        int k=0;        for(int i=9;i>=0;i--)        {            for(int j=0;j<res[i];j++) {="" s[k]="0" +i;="" k++;="" }="" }="" return="" s;="">
0
AKS9 months ago
AKS
def LargestEven(self, S):        # code herel = list(map(int, list(S)))        d = []        for i in l:            if i % 2 == 0:                d.append(i)
        if len(d):            l.remove(min(d))            l = sorted(l, reverse = True)            l.append(min(d))            return ''.join(list(map(str, l)))        else:            l = sorted(l, reverse = True)            return ''.join(list(map(str, l)))
0
Mansi Garg9 months ago
Mansi Garg
C++ SolutionExecution Time: 0.5void swap(char &a, char &b){        char t = a;        a = b; b = t;    }    string LargestEven(string S){        //complete the function here        sort(S.begin(), S.end(), greater<int>());        bool even = false;        for(int i=0; i<s.size(); i++){="" if((s[i]-'0')%2="=0){" even="true;" break;="" }="" }="" if(even="=true" &&="" (s[s.size()-1]-'0')%2!="0){" for(int="" i="S.size()-2;" i="">=0; i--){            if((S[i]-'0')%2==0){                swap(S[i], S[S.size()-1]);                sort(S.begin(), S.begin()+S.size()-1, greater<int>());                break;            }        }        }        return S;    }  
0
Ashish Dhakad10 months ago
Ashish Dhakad
SIMPLE C++ SOLUTION
class Solution{    public:    string LargestEven(string S){         sort(S.begin(),S.end(),greater<>());         int n=S.length();         int l=n-1;         for(int i=n-1;i>=0;i--)         {             if(S[i]%2==0){                 l=i;                 break;             }         }         int temp=S[l];         S[l]=S[n-1];         sort(S.begin(),S.end(),greater<>());         S[n-1]=temp;
        return S;
0
Anwesan De10 months ago
Anwesan De
Python solution
https://p.ip.fi/UOKV
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.