Type the Question

Saturday, February 16, 2019

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

No comments:

Post a Comment

Question Name:TOWER OF HANOI

#include < bits / stdc ++. h > #define lli long long using namespace std ; lli dp [ 202 ]; int main () { int t , n ; ...