From 7907135fa4dc41129095a14db5921a52e2688a92 Mon Sep 17 00:00:00 2001 From: Harikrishna <70622800+hryxna@users.noreply.github.com> Date: Sat, 3 Oct 2020 21:08:58 +0530 Subject: [PATCH 1/2] Added README.md file of Maximum Element Problem --- .../Stack/Maximum Element/README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Practice Problems/Stack/Maximum Element/README.md 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
From a214f67feef14ebb8cb5b485b94dfde00c7f531a Mon Sep 17 00:00:00 2001 From: Harikrishna <70622800+hryxna@users.noreply.github.com> Date: Sat, 3 Oct 2020 21:12:58 +0530 Subject: [PATCH 2/2] Added Maximum Element Solution --- .../Stack/Maximum Element/MaximumElement.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Practice Problems/Stack/Maximum Element/MaximumElement.java 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(); + } +}

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