-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (41 loc) · 1.64 KB
/
Solution.java
File metadata and controls
49 lines (41 loc) · 1.64 KB
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
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.util.stream.Collectors;
public class Solution {
private static final String DELETE = "Delete";
private static final String INSERT = "Insert";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sizeOfList = scanner.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < sizeOfList; i++) {
list.add(scanner.nextInt());
}
int numberOfQueries = scanner.nextInt();
for (int i = 0; i < numberOfQueries; i++) {
String operation = scanner.next().trim();
scanner.nextLine();
String input = scanner.nextLine().trim();
executeQuery(list, operation, input);
}
System.out.println(String.join(" ", list.stream()
.map(s -> String.valueOf(s))
.collect(Collectors.toList())));
scanner.close();
}
public static void executeQuery(List<Integer> list, String operation, String input) {
if (operation.equals(DELETE)) {
list.remove(Integer.parseInt(input));
} else if (operation.equals(INSERT)) {
String[] splitString = input.split(" ");
int index = Integer.parseInt(splitString[0]);
int value = Integer.parseInt(splitString[1]);
if (index > list.size()) {
list.add(value);
} else {
list.add(index, value);
}
} else {
throw new Error(String.format("Unsupported operation %s", operation));
}
}
}