Get minimum of stack in Reasonable time

// what if a product based  company asks that they want the minimum of stack in resonalble time..
// if you knew this program congrats... you got job in product based company
#include<iostream>
#include<stack>
using namespace std;
void push(int a);
bool isFull(int n);
bool isEmpty();
int pop();
int getMin();

stack<int> s;
int main(){
int t;
cin>>t;
while(t--){
int n,a;
cin>>n;
while(!isEmpty()){
    pop();
}
while(!isFull(n)){
cin>>a;
push(a);
}
cout<<getMin()<<endl;
}
}

/*This is a function problem.You only need to complete the function given below*/
/*Complete the function(s) below*/
void push(int a)
{
     s.push(a);
}
bool isFull(int n)
{
    return (s.size()==n);
}
bool isEmpty()
{return s.empty();
   
}
int pop()
{
    int a=s.top();
    s.pop();
    return a ;
   
}
int getMin()
{
    if(!s.empty())
{
    int min= s.top();
    s.pop();
   while(!s.empty())
   {if(min>s.top())
   {
       min=s.top();
       s.pop();
   }
   
   }
   return min;
}

}

Comments

Popular posts from this blog

Linked List 10 operations from which problems are made ( complete guide for you)

Stack - print bracket number