We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int n=sc.nextInt();
Stack<Integer> s=new Stack<>();
Stack<Integer> max=new Stack<>();
max.push(0);
while(n-->0)
{
int a=sc.nextInt();
if(a==1)
{
int b=sc.nextInt();
s.push(b);
if(b>=max.peek())
max.push(b);
}
else if(a==2)
{
if(s.peek()==max.peek())
max.pop();
s.pop();
}
else
{
System.out.println(max.peek());
}
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
public class Solution {
}