Unable to understand which testcase I am missing (SWPDGT)

SWPDGT - SWPDGT Problem - CodeChef

Below is my code for the question, I am unable to understand why am I getting wrong answer. Which testcase am I missing ?

#include<bits/stdc++.h>
using namespace std;
void swapOnce(int a, int b){
    int o1, t1, o2, t2;
    o1 = a%10;
    t1 = a/10;
    
    o2 = b%10;
    t2 = b/10;
    int total = 0;
    //cout<<"\nBefore swap "<<t1<<o1<<" "<<t2<<o2;
    if(t1 == 0 && t2 == 0){
        //reconstruct the number
        cout<<"\nAfter swap "<<t1<<o1<<" "<<t2<<o2;
        total = ((t1+t2)*10) + (o1+o2);
        cout<<"\n"<<total;
    }
    else
    {
        if(o1 > t2 && o2<=t1){
            int temp = o1;
            o1 = t2;
            t2 = temp;
        }
        else if(o2 > t1 && o1<=t2){
            int temp = o2;
            o2 = t1;
            t1 = temp;
        }
        else if(o1 > t2 && o2 > t1)
        {
            if((o1 - t2) > (o2 - t1)){
                int temp = o1;
                o1 = t2;
                t2 = temp;
            }
            else
            {
                int temp = o2;
                o2 = t1;
                t1 = temp;
            }
        }
        else
        {
            //do nothing
        }
        //reconstruct the number
        cout<<"\nAfter swap "<<t1<<o1<<" "<<t2<<o2;
        total = ((t1+t2)*10) + (o1+o2);
        cout<<"\n"<<total; 
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    // cout<<"\nEnter the testcases";
    cin>>t;
    while (t--)
    {
        int a, b;
        cin>>a>>b;
        swapOnce(a,b);

    }
    return 0;
}

You shouldn’t output texts like “Before swap”…
Just output in the format it is asked

U better use brute method by swapping 2 digits of numbers and add all possible

I commented that out while submitting, will make the changes here too

I agree, but I would like to know which testcase this implementation is missing.

For the values 1 99 your code is giving output 181 but it should be 100

1 Like

Thank you. But why 100 ? Isnt the goal to maximize the sum ?

@karan_0902 The maximum sum is 100.
As 1 + 99 = 100
if you sawp 1 and 9 than it would be 9 + 91 = 100.
so 100 is correct.

In case of 1 and 99 if you swap the numbers it will either give 91 and 9 or 19 and 9. In case of 91 and 9 sum is 100 as in the case of 1 and 99. Hence maximum answer is 100. What your code is doing it is actually taking 01 and 99 and hence replacing the digits to 90 and 91.

1 Like

Yes, I misunderstood the question and added a zero in front of every one digit number.

Yes, I misunderstood the question and added a zero in front of every one digit number. Thanks.

Welcome. :slightly_smiling_face: