Question Name:Largest Even Number

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
	int i,n,m,j,k,l,o,p;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
	    int U = 0;
	    char str[100000];
	    scanf("%s",str);
	    int max = 99999;
	    unsigned long long int a[10]={0};
	    for(j=0;j<strlen(str);j++)
	    {
	        int Y = str[j]-48;
	        if(Y%2==0)
	        {
	            U=1;
	            if(Y<max)
	            {
	                max=Y;
	            }
	        }
	        a[Y]++;
	    }
	    if(U==0)
	    {
	        for(j=10-1;j>=0;j--)
	        {
	            if(a[j]!=0)
	            {
	                for(k=0;k<a[j];k++)
	                {
	                    printf("%d",j);
	                }
	            }
	        }
	    }
	    else
	    {
	        for(j=10-1;j>=0;j--)
	        {
	            if(a[j]!=0 && j!=max)
	            {
	                for(k=0;k<a[j];k++)
	                {
	                    printf("%d",j);
	                }
	            }
	            else if(a[j]!=0 && j==max)
	            {
	                for(k=0;k<a[j]-1;k++)
	                {
	                    printf("%d",j);
	                }
	            }
	        }
	        printf("%d",max);
	    }
	    printf("\n");
	}
  return 0;
}

Problem Description

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

  • Test Case 1

    Input (stdin)

    5
    1324
    3415436
    1023422
    03517
    3555
    

    Expected Output

    4312
    6543314
    4322210
    75310
    5553
  • Test Case 2

    Input (stdin)

    3
    315436
    1422
    73555
    

    Expected Output

    653314
    4212
    75553

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.