diff --git a/Practice Problems/Stack/Maximum Element/MaximumElement.java b/Practice Problems/Stack/Maximum Element/MaximumElement.java new file mode 100644 index 0000000..c8fcf92 --- /dev/null +++ b/Practice Problems/Stack/Maximum Element/MaximumElement.java @@ -0,0 +1,38 @@ +import java.io.*; +import java.util.*; + +public class MaximumElement { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + Stack maxStack = new Stack(); + ArrayDeque stack = new ArrayDeque(); + for(int i=0;i=maxStack.peek()){ + maxStack.push(val); + } + break; + + case 2 : int popped = stack.pop(); + if(popped == maxStack.peek()){ + maxStack.pop(); + } + break; + + case 3 : + System.out.println(maxStack.peek()); + break; + + default: break; + } + } + sc.close(); + } +} diff --git a/Practice Problems/Stack/Maximum Element/README.md b/Practice Problems/Stack/Maximum Element/README.md new file mode 100644 index 0000000..57466bd --- /dev/null +++ b/Practice Problems/Stack/Maximum Element/README.md @@ -0,0 +1,28 @@ +# Maximum Element +### Problem from Hackerrank Practice> DataStructures> Stacks + +You have an empty sequence, and you will be given **N** queries. Each query is one of these three types: + +- 1 x -Push the element x into the stack. +- 2 -Delete the element present at the top of the stack. +- 3 -Print the maximum element in the stack. + +The first line of input contains an integer, **N** . The next **N** lines each contain an above mentioned query. (It is guaranteed that each query is valid.) + +## Example: +### Input:
+10
+1 97
+2
+1 20
+2
+1 26
+1 20
+2
+3
+1 91
+3
+ +### Output:
+26
+91

AltStyle によって変換されたページ (->オリジナル) /