Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16175cd

Browse files
authored
Merge pull request #75 from hryxna/master
Adding a Hackerrank Problem along with the Solution to Stack Folder
2 parents 7f477f3 + a214f67 commit 16175cd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class MaximumElement {
5+
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
Stack<Integer> maxStack = new Stack<>();
10+
ArrayDeque<Integer> stack = new ArrayDeque<>();
11+
for(int i=0;i<n;i++){
12+
13+
int c = sc.nextInt();
14+
15+
switch (c){
16+
case 1 : int val = sc.nextInt();
17+
stack.push(val);
18+
if(maxStack.isEmpty() || val>=maxStack.peek()){
19+
maxStack.push(val);
20+
}
21+
break;
22+
23+
case 2 : int popped = stack.pop();
24+
if(popped == maxStack.peek()){
25+
maxStack.pop();
26+
}
27+
break;
28+
29+
case 3 :
30+
System.out.println(maxStack.peek());
31+
break;
32+
33+
default: break;
34+
}
35+
}
36+
sc.close();
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Maximum Element
2+
### Problem from Hackerrank Practice > DataStructures > Stacks
3+
4+
You have an empty sequence, and you will be given **N** queries. Each query is one of these three types:
5+
6+
- 1 x -Push the element x into the stack.
7+
- 2 -Delete the element present at the top of the stack.
8+
- 3 -Print the maximum element in the stack.
9+
10+
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.)
11+
12+
## Example:
13+
### Input:<br>
14+
10<br>
15+
1 97<br>
16+
2<br>
17+
1 20<br>
18+
2<br>
19+
1 26<br>
20+
1 20<br>
21+
2<br>
22+
3<br>
23+
1 91<br>
24+
3<br>
25+
26+
### Output:<br>
27+
26<br>
28+
91<br>

0 commit comments

Comments
(0)

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