SWPDGT - March Lunchtime 2020 (Unrated) VIDEO solution

Solution for SWPDGT :- Video Solution
Please tell me if there is something wrong in the video

Edit :- I forgot to read that we can perform operation at most once. So, I have solved the problem by performing any number of operations to maximize the sum. I think this is more hard though :sweat_smile:. Sorry for the inconvenience.

Thank you and happy coding

Well, you will better know whats wrong with your college😂

1 Like
int main(){
fastIO
lli t=1; 
cin>>t;
while(t--) {
string a,b;
cin>>a>>b;
int maxi =-1;
for(lli i=0;i<sz(a);i++)
{
for(lli j=0;j<sz(b);j++){
swap(a[i],b[j]);
int aa = stoi(a);
int bb = stoi(b);
maxi = max(maxi , (aa+bb));
swap(a[i],b[j]);
 }
}
cout<<maxi<<endl;
}
return 0;
}

That’s what I did … Basically I want an approach on how to solve the same question if the two numbers are the numeric strings of length upto 10^5 .
@ssjgz @l_returns @anon79763389

Create hash of digits in A and B separately.
Now start traversing A from left and check whether you can swap it with any large digit having lowest place value possible in B. If you can swap, then, swap with the largest digit possible and break out of the loop. Remember that you can swap only if the digit in B have lower place value than digit in B. So,create your hash accordingly.
Similarly do it for B.
Now, out of the above 2 swaps, you have to choose the one which is performed at the digit having higher place value.

A = 234, B = 929

After applying first step A = 934 and B = 922
After applying second step A = 232 and B = 949

So, you have to choose first swap

This is a rough idea. Please confirm its correctness