-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaList.java
More file actions
71 lines (50 loc) · 1.51 KB
/
Copy pathJavaList.java
File metadata and controls
71 lines (50 loc) · 1.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Question:
Given a list of integers, perform the following queries:
1. Insert x y
-> Insert value y at index x.
2. Delete x
-> Delete the element at index x.
After performing all queries, print the final list.
*/
import java.util.*;
public class Solve {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read initial size of the list
int n = sc.nextInt();
// Create ArrayList
ArrayList<Integer> list = new ArrayList<>();
// Read list elements
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
// Read number of queries
int q = sc.nextInt();
// Process each query
for (int i = 0; i < q; i++) {
// Read query type
String query = sc.next();
// Insert operation
if (query.equals("Insert")) {
// Read index and value
int index = sc.nextInt();
int value = sc.nextInt();
// Insert value at given index
list.add(index, value);
}
// Delete operation
else if (query.equals("Delete")) {
// Read index to delete
int index = sc.nextInt();
// Remove element at given index
list.remove(index);
}
}
// Print final list
for (int num : list) {
System.out.print(num + " ");
}
sc.close();
}
}