Largest Even Number

PROBLEM :
As usual Babul is back with his problem but this time with numbers. In his problem there is a number P (always a whole number) with N digits. Now he started finding out the largest possible even number formed by rearranging this N digit number. For example consider number 1324, after rearranging the digits the largest even number possible is 4312.

Note: In case the number does not contain any even digit then output the largest odd number possible.

Input:
The first line of input will contain an integer T which is the no. of test cases. Each of the next T lines will contain a number P.

Output:
For each test case in a new line print the required result.

Constraints:
1<=T<=100
1<=N<=1000

Example:

Input:
5
1324
3415436
1023422
03517
3555

Output:
4312
6543314
4322210
75310
5553

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
#include <string>
#include <stdlib.h>
string Largest_Even_Number(string) ;
string sort(string,int ) ;
int main()
 {
int t ;
string str ;
cin>>t ;
while(t--)
{
   cin>>str ;
   str=Largest_Even_Number(str) ;
   cout<<str<<endl ;
}
return 0;
}

string Largest_Even_Number(string str)
{
    int len ;
    len=str.length() ;
   
    str=sort(str,len) ;
   
    if((str[len-1]-'0')%2==0)
        return str;
       
    int i,pos ;
    for(i=len-1;i>=0;i--)
    {
       if(((str[i])-'0')%2==0)
        {
            pos=i ;
            break ;
        }
    }
   
    int temp ;
    for(i=pos;i<len-1;i++)
    {
        temp=str[i] ;
        str[i]=str[i+1] ;
        str[i+1]=temp ;
    }
   
    return str ;
}

string sort(string str,int no)
{
    int i,j;
    char temp ;
   
    for(i=no-1;i>0;i--)
    {
        for(j=no-1;j>0;j--)
        {
            if(str[j]>str[j-1])
            {
                temp=str[j] ;
                str[j]=str[j-1] ;
                str[j-1]=temp ;
            }
        }
    }
    return str ;
}

// we can use better sorting algo

---------------------------------------------------------------------------------
USING STL Library
---------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include <string>
#include <stdlib.h>
#include <algorithm>
string Largest_Even_Number(string) ;
bool wayToSort(int i, int j) { return i > j; }
int main()
 {
int t ;
string str ;
cin>>t ;
while(t--)
{
   cin>>str ;
   str=Largest_Even_Number(str) ;
   cout<<str<<endl ;
}
return 0;
}

string Largest_Even_Number(string str)
{
    int len ;
    len=str.length() ;
   
    std::sort( str.begin(), str.end() ,wayToSort) ;
   
    if((str[len-1]-'0')%2==0)
        return str;
       
    int i,pos ;
    for(i=len-1;i>=0;i--)
    {
       if(((str[i])-'0')%2==0)
        {
            pos=i ;
            break ;
        }
    }
   
    int temp ;
    for(i=pos;i<len-1;i++)
    {
        temp=str[i] ;
        str[i]=str[i+1] ;
        str[i+1]=temp ;
    }
   
    return str ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )