i have a doubt on the fact that u have given a formulae for implementing it using a stack. i have implemented it using two stack is it okay for find maximum at any point of time pushing and popping element into or from the stack.
source code:
#include<bits/stdc++.h>
using namespace std;
void pushh(int x,stack &a,stack &b)
{
if(a.empty()&& b.empty())
{
a.push(x);
b.push(x);
}
if(x>=b.top())
{
b.push(x);
}
else
{
int u=b.top();
b.push(u);
}
a.push(x);
}
void popp(stack&s1,stack&s2)
{
if(s1.empty()&&s2.empty())
{
cout<<“underflow”<<endl;
return;
}
s1.pop();
s2.pop();
}
void prntmax(stack &s2)
{
cout<<"the highest no. is "<<s2.top();
}
int main()
{
stacks1;
stacks2;
int n;
cin>>n;
while(n--)
{
int y;
cin>>y;
pushh(y,s1,s2);
}
popp(s1,s2);
popp(s1,s2);
prntmax(s2);
return(0);
}