Maximum element in a stack java : <p><a href="https://www.hackerrank.com/challenges/maximum-element" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/maximum-element</a></p>

<p>ques is to perform three queries 1-for adding elment to stack 2-for poping element 3-for printing max element
link is posted above
iam not able to get output for 6-7 cases on hackerrank</p>

<pre><code>    int n,i;int in1,in2;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    Stack&lt;Integer&gt; st=new Stack&lt;Integer&gt;();//stack for elements
    Stack&lt;Integer&gt; stmax=new Stack&lt;Integer&gt;(); // stack for storing maximum
        for(i=1;i&lt;=n;i++)
        {
        in1=sc.nextInt();
        if(in1==1)
            {
                     in2=sc.nextInt();
                     if(st.size()==0)
                       {

                              stmax.push(in2);
                              st.push(in2);
                       }
                     else
                         {
                              if(in2&gt;=stmax.peek())
                                  {

                              stmax.push(in2);
                              }
                               st.push(in2);

                          }    
        }
        if(in1==2)
            {
            if(st.peek()==stmax.peek())
                stmax.pop();
            st.pop();

        }
        if(in1==3)
            {
            System.out.println(stmax.peek());
        }

        }
</code></pre>
