Write a program to implement a Stack using an array. Complete the push() and pop() methods as described below. The push(x) method takes an integer x as input and adds it to the stack. The pop() method removes and returns the top element of the stack. If the stack is empty, the pop() method should return -1.
Input is provided as a series of queries:
Example 1
Input: 1 5 1 15 2 2 2
Output: 15 5 -1
Explanation: The first query 1 5 means push 5 onto the stack. The stack becomes [5]. The second query 1 15 means push 15 onto the stack. The stack becomes [5, 15]. The third query 2 means pop the top element, which is 15. The stack becomes [5]- Output 15. The fourth query 2 means pop the top element, which is 5. The stack becomes []- Output 5. The fifth query 2 means pop the top element, but the stack is empty. Output -1.
Example 2
Input: 1 10 1 20 2 2
Output: 20 10
Explanation: The first query 1 10 means push 10 onto the stack. The stack becomes [10]. The second query 1 20 means push 20 onto the stack. The stack becomes [10, 20]. The third query 2 means pop the top element from the stack, which is 20. The stack becomes [10]- Output 20. The fourth query 2 means pop the top element from the stack, which is 10. The stack becomes []- Output 10.
Sign in to write, run, and submit your solution against the full test suite.