Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class stack_Maximum_Element {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = scanner.nextInt();
- ArrayDeque<Integer> stack = new ArrayDeque<>();
- for (int i = 0; i < num; i++) {
- int command = scanner.nextInt();
- if (command == 1) {
- stack.push(scanner.nextInt());
- } else if (command == 2) {
- if (!stack.isEmpty()){
- stack.pop();
- }
- } else if (command == 3) {
- int max = Integer.MIN_VALUE;
- int temp = 0;
- int count = stack.size();
- if (!stack.isEmpty()){
- while (count > 0) {
- temp = stack.pop();
- stack.addLast(temp);
- count--;
- if (temp > max) {
- max = temp;
- }
- }
- System.out.println(max);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement