Problem FLOW017

problem while solving FLOW017
#include
using namespace std;
int main (){
int T,A,B,C,M,m;
cin >> T;
while (T–){cin >>A>>B>>C;
{ M= A;
if (B > M){M=B;}
if (C > M){M=C;}
}
{m= A;
if (B < m){M=B;}
if (C < m){M=C;}}
{if (A!= M && A!=m) cout<<A<<endl;
if (B!= M && B!=m) cout<<B<<endl;
if (C!= M && C!=m) cout<<C<<endl;}
cout <<endl;}
return 0;
}
Why is this wrong

    int a, b, c;
    cin >> a >> b >> c;
    if(a > b && a > c) {
        if(b > c) {
            cout << b;
        }
        else {
            cout << c;
        }
    }
    else if(b > a && b > c) {
        if(a > c) {
            cout << a;
        }
        else {
            cout << c;
        }
    }
    else {
        if(a > b) {
            cout << a;
        }
        else {
            cout << b;
        }
    }
    cout << "\n";

This should work. And btw, if I were in that place, I would have taken all three into a list, sort and print the 2^{nd} item.

2 Likes

The problem does not state that numbers must be unique. Therefore in cases of two equal numbers or three equal numbers you’re printing wrong answer.

This could be solved by covering special cases, but as @suman_18733097 said, there are multiple different ways to generalize the problem for bigger inputs in O(N log N) complexity. Some of them would be using priority_queue or a set, but probably the easiest one to simply store an array of elements, sort it in non-increasing manner and print the Kth element, in your case second.

1 Like

Thankyou

2 Likes