CHEF SWAPS DIGITS(LUNCHTIME)


19h

Actually i am not getting a question of march lunchtime.
here i am providing the link of that question.
plz explain with example of 99 and 9 ,58 and 2.

CodeChef

Chef Swaps Digits | CodeChef

Chef Swaps Digits | CodeChef

@everule1 @thunderbird001 @everule1 @lovishagg @kill_or_kill @vansh_kalra

So, what the chef wants you to do is find the max sum. They are providing you one way to change the pair of numbers by swapping on digit so that the sum can be maximized.
Now consider 99 and 9. sum is 99 + 9 = 108.
Can we increase this sum by swapping any of the digit? No, because swapping any digit gets you 99 & 9 again. so max sum is 108.

58 and 2 is a bad example cuz max sum is sum of original numbers 60.

Instead take 58 and 70. Sum = 128. Can we increase this? Yes we can by swapping 8 and 7. so it becomes 57 and 80. And the new sum is 137 also the maximum.
Note - we can do this swap only once. Hope you get it.

@tomkasetty thanks for explaining it in such a nice way thanks a lot :hugs: :hugs: :hugs: :hugs:

is there any inbuilt function to find maximum of two or three numbers???

1 Like

max({a,b,c});
In
#include<algorithm>
Or
#include<bits/stdc++.h>

@everule1
Rashmi Jain mam my code is not working
"
#include<bits/stdc++.h>
#define n 10
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a,b;
cin>>a>>b;

    if(a<10&&b<10)//comparing with 10 basically means checking digit;
    cout<<a+b<<endl;
    else
    if(a<10&&b>=10)
    cout<<max(a+b,10*a+(b%10)+(b/10))<<endl;
    else
    if(a>=10&&b<10)
    cout<<max(10*b+(b%10)+(b/10),a+b)<<endl;
    else
    if(a>=10&&b>=10)

{
cout<<max(a+b,10*(b%10)+(a%10),10*(b/10)+(a/10),10*(a/10)+(b/10),10*(a%10)+b%n);
cout<<"\n";
}
}
}
"

At least use ``` above and below the code on forum to format it correctly.

1 Like

@everule1 check if now it’s okay?

"#include<bits/stdc++.h>
#define n 10
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a,b;
cin>>a>>b;

    if(a<10&&b<10)//comparing with 10 basically means checking digit;
    cout<<a+b<<endl;
    else
    if(a<10&&b>=10)
    cout<<max(a+b,10*a+(b%10)+(b/10))<<endl;
    else
    if(a>=10&&b<10)
    cout<<max(10*b+(b%10)+(b/10),a+b)<<endl;
    else
    if(a>=10&&b>=10)

{
cout<<max(a+b,10*(b%10)+(a%10),10*(b/10)+(a/10),10*(a/10)+(b/10),10*(a%10)+b%n);
cout<<"\n";
}
}
}"

You haven’t even put curly brackets around the last max. To take maximum of multiple elements use
max({a,b,c})

@everule1 okay thanku mam now it’s compiling my program

mam actually you in which college ??
are u a student or work ??

I’m in 11th.

in class 11th???

1 Like

wow!!!great to see you are such an amazing programmer at such a young age