Untitled

                Never    
Java
       
package JavaAdvanced.StacksAndQueues.Ex;

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Scanner;

public class MaximumElement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int countCommands = Integer.parseInt(scanner.nextLine());
        ArrayDeque<String> stackWithElements = new ArrayDeque<>();


        for (int i = 0; i < countCommands; i++) {
            String[] tokens = scanner.nextLine().split("\\s+");
            String command = tokens[0];

            switch (command) {
                case "1":
                    String element = tokens[1];
                    stackWithElements.push(element);
                    break;
                case "2":
                    stackWithElements.pop();
                    break;
                case "3":
                    int biggest = Integer.parseInt(stackWithElements.peek());
                    for (int j = 0; i < stackWithElements.size(); i++) {
                        int last = Integer.parseInt(stackWithElements.pop());
                        stackWithElements.push(String.valueOf(last));
                        if (biggest < Integer.parseInt(stackWithElements.peek())) {
                            biggest = Integer.parseInt(stackWithElements.peek());
                        }
                    }
                    System.out.println(biggest);
                    break;
            }
        }
    }
}

Raw Text