-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicStackOperations.java
38 lines (36 loc) · 1.16 KB
/
BasicStackOperations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package StackAndQueues_Exercises;
import java.util.ArrayDeque;
import java.util.Scanner;
public class BasicStackOperations {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int N = Integer.parseInt(scan.next()); // number to be added
int S = Integer.parseInt(scan.next()); //number of el to be popped
int X = Integer.parseInt(scan.next()); // is it in the stack
ArrayDeque <Integer> stack = new ArrayDeque<>();
while(N > 0){
int num = Integer.parseInt(scan.next());
stack.push(num);
N--;
}
while(S > 0){
stack.pop();
S--;
}
if(stack.contains(X)) {
System.out.println("true");
}else{
int min = Integer.MAX_VALUE;
if(stack.isEmpty()){
System.out.println(0);
}else {
for (int number : stack) {
if (number < min) {
min = number;
}
}
System.out.println(min);
}
}
}
}