Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Monday, February 5, 2018

Longest Even Number


Task is to form longest even number using digits of given number.

Idea is to use Count sort to sort them and them find the least even number if 0th index is not even.

Print them.


Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class dmg
 {
public static void longestEvenNumber(String str)
{
//count Sort
int len=str.length();
int count[]=new int[10];
for(int i=0;i<len;++i)
{
++count[str.charAt(i)-'0'];
}
for(int i=1;i<10;++i)
count[i]+=count[i-1];
int op[]=new int[len];//output array
for(int i=0;i<len;++i)
{
op[count[str.charAt(i)-'0']-1]=str.charAt(i)-'0';
--count[str.charAt(i)-'0'];
}
int ev=-1;
//Look for next even number and save it
if(((op[0])&1)!=0)
for(int i=1;i<len;++i)
{
    // System.out.println("lop");
if((op[i]&1)==0)
{
ev=op[i];
op[i]=-1;
break;
}
}
for(int i=len-1;i>=0;--i)
{
if(op[i]!=-1)
System.out.print(op[i]);
}
if(ev!=-1)
System.out.print(ev);
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
longestEvenNumber(ab.next());
    System.out.println();
}
}
}

0 Comments:

Post a Comment

Stats