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

Adding a Hackerrank Problem along with the Solution to Stack Folder #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
srsandy merged 2 commits into srsandy:master from hryxna:master
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Practice Problems/Stack/Maximum Element/MaximumElement.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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<Integer> maxStack = new Stack<>();
ArrayDeque<Integer> stack = new ArrayDeque<>();
for(int i=0;i<n;i++){

int c = sc.nextInt();

switch (c){
case 1 : int val = sc.nextInt();
stack.push(val);
if(maxStack.isEmpty() || val>=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();
}
}
28 changes: 28 additions & 0 deletions Practice Problems/Stack/Maximum Element/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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:<br>
10<br>
1 97<br>
2<br>
1 20<br>
2<br>
1 26<br>
1 20<br>
2<br>
3<br>
1 91<br>
3<br>

### Output:<br>
26<br>
91<br>

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