lundi 1 juillet 2019

My code is not printing max but when it is in the loop it is printing max. Why?

I've doing hackerrank problem to find max element in a stack https://www.hackerrank.com/challenges/maximum-element/problem In my code I've to assign max element as max when I move cout<<max; out of the loop, it isn't showing max element while it is showing when it is inside the loop

I've tried to make function but still it doesn't working.

stack<ll> s1, s2;
ll x;
while (cin >> x)
{
    if (s1.empty() && s2.empty())
    {
         cout << "Start" << endl;
         s1.push(x);
         s2.push(x);
    }
    else
    {
         cout << "ELSE" << endl;
         s1.push(x);
         if (s1.top() >= s2.top())
         {
             s2.push(s1.top());
         }
         cout << "end" << endl;
    }
}
ll max = s2.top();
cout << "max is " << max;

No error message

Aucun commentaire:

Enregistrer un commentaire