title: FInd minimum element in stack in O(1) time C++
body: <p>Here I am finding the minimum element in a stack using <code>mini()</code> function. While pushing and popping in the stack, I set <code>min1</code> and <code>min2</code> logically. In which cases would my code fail? What are the problems with my code?</p>
  
  <p>The push function sets <code>min1</code> and <code>min2</code> variables. <code>min1</code> corresponds to the least element and <code>min2</code> corresponds to 2nd least element in the stack. In the pop if popped element is equal to mini then I update <code>min1</code> to 2nd least value before pop and carry on. So at all times <code>min1</code> has least value in the stack.   </p>
  
  <pre><code>class stac
  {
  public:
  void push(int item)
  {
  if(top&gt;=STACK_SIZE-1)
  {
      cout&lt;&lt;"Full"&lt;&lt;endl;
      return;
  }
  else
  {
      if(item&lt;min1)
      {
          min2 = min1;
          min1=item;
      }
      s[++top]=item;
      return;
  }
  }
  
  void pop()
  {
  if(top==-1)
  {
      cout&lt;&lt;"Empty"&lt;&lt;endl;
      return;
  }
  else
  {
      if(s[top]==min1)
      {
          min1=min2;
      }
      top--;
      return;
  }
  }
  void mini()
   {
     if(top==-1)
    {
        cout&lt;&lt;"no minimum"&lt;&lt;endl;
        return;
  }
  else
  {
      cout&lt;&lt;min1&lt;&lt;endl;
  }
  }
  
  
  private:
  int min1 = INT_MAX;
  int min2;
  int s[STACK_SIZE];
  int top = -1;
  };
  int main()
  {
  
    stac s1;
  
    s1.push(5);
    s1.push(2);
    s1.push(9);
    s1.push(1);
    s1.push(24);
    s1.push(-1);
    s1.push(-87);
    s1.push(23);
    s1.mini();
    s1.display();
  
    return 0;
  }
  </code></pre>